javaswinglayout-manager

Problem with aligning component using Java swing and layout managers


I am trying learn java swing. I am having troubles, and I think it is caused by the layout managers, the way I use them.

In the image attached below, you see the JList which is added to the JSchrollPane (The one where the text says XX). The list is very small in height, I can't make it higher. There is plenty of space for it, and there are 10 items in the list. I think it could show more than 1 item at a time.

The most relevant code for the panels and other components is pointed out below.

Another thing, is the huge button below. I don't know why it takes up so much space. I have tried to put it in different panels, and also directly adding it through the JFrame, but I don't get any good results. I just want it to look smaller, and be at the bottom of the Frame. If the button is pushed down, there will be plenty of space for the JList to be higher/bigger. Any help is appreciated.

Below is the entire code that creates the GUI. It can be copied and run. Thanks

panCourseCheckboxes = new JPanel(new GridLayout(4, 1));
manageCourseTitleBorder = new TitledBorder("Manage Course");
addStudTitledBorder.setTitleColor(Color.RED);
manageCourseTitleBorder.setTitleColor(Color.RED);
panCourseCheckboxes.setBorder(manageCourseTitleBorder);
addCourseCheckBox = new JCheckBox("Attach student to course");      
labAllCourses = new JLabel("Courses");  
    
panCourseCheckboxes.add(addCourseCheckBox);
panCourseCheckboxes.add(labAllCourses);
panCourseCheckboxes.add(courseSchrollPane);

enter image description here

import java.awt.*;

import javax.swing.*;
import javax.swing.border.TitledBorder;
public class SwingTutorial {

    private JFrame frame;
    private JPanel panAddStud, panCourseCheckboxes;
    private JTextField tFieldAddStudFirstName, tFieldAddStudStudLastName, tFieldAddStudEmail;
    private JLabel labelAddStudFirstName, labelAddStudLastName, labelAddStudEmail, labAllCourses;
    private JButton addStudBut;
    private JCheckBox addCourseCheckBox;
    private JList<String> coursesList;
    //private JSeparator separator;
    private JScrollPane courseSchrollPane;  
    private Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    private TitledBorder addStudTitledBorder, manageCourseTitleBorder;

    private int frameWidth = screenSize.width/3, 
            frameHeight = screenSize.height/2;
    private int textFieldWidth = 20,
            textFieldHeight = 10;           
    private int panTextFieldWidth = screenSize.width/3,
            panTextFieldHeight = screenSize.height/3;
    
    public SwingTutorial() {
        frame = new JFrame("Manage course");
        frame.setLayout(new GridLayout(4, 2));
        frame.pack();       
        frame.setSize(frameWidth, frameHeight);

        panAddStud = new JPanel(new GridLayout(3, 2));
        //panAddStud = new JPanel();
        panAddStud.setSize(panTextFieldWidth, panTextFieldHeight);
        addStudTitledBorder = new TitledBorder("Add student");          
        addStudTitledBorder.setTitleColor(Color.RED);
        panAddStud.setBorder(addStudTitledBorder);
        
        //panStudentBut = new JPanel(new GridLayout());
        
        labelAddStudFirstName = new JLabel("First name");
        labelAddStudLastName = new JLabel("Last name");
        labelAddStudEmail = new JLabel("Email");
        
        tFieldAddStudFirstName = new JTextField(); 
        tFieldAddStudFirstName.setSize(textFieldWidth, textFieldHeight);        

        tFieldAddStudStudLastName = new JTextField();
        tFieldAddStudStudLastName.setSize(textFieldWidth, textFieldHeight);

        tFieldAddStudEmail = new JTextField();
        tFieldAddStudEmail.setSize(textFieldWidth, textFieldHeight);

        addStudBut = new JButton("Add student");
        
        panCourseCheckboxes = new JPanel(new GridLayout(4, 1));
        manageCourseTitleBorder = new TitledBorder("Manage Course");
        addStudTitledBorder.setTitleColor(Color.RED);
        manageCourseTitleBorder.setTitleColor(Color.RED);
        panCourseCheckboxes.setBorder(manageCourseTitleBorder);
        addCourseCheckBox = new JCheckBox("Attach student to course");      
        labAllCourses = new JLabel("Courses");      
        
        String tempL[] = { "XX", "YY", "BB", "TT", "OO", "VV", "EE", "LL", "PP", "PP" };        
        coursesList = new JList<String>(tempL);
        courseSchrollPane = new JScrollPane(coursesList);
        courseSchrollPane.setPreferredSize(new Dimension(20, 100));
    }
    
    public void createGUI() {
        panAddStud.add(labelAddStudFirstName);
        panAddStud.add(tFieldAddStudFirstName);

        panAddStud.add(labelAddStudLastName);
        panAddStud.add(tFieldAddStudStudLastName);

        panAddStud.add(labelAddStudEmail);
        panAddStud.add(tFieldAddStudEmail);         

        frame.add(panAddStud);
        //frame.add(separator);
        
        panCourseCheckboxes.add(addCourseCheckBox);
        panCourseCheckboxes.add(labAllCourses);
        panCourseCheckboxes.add(courseSchrollPane);
        
        frame.add(panCourseCheckboxes);     

        //panStudentBut.add(addStudBut);
        
        frame.add(addStudBut);
        //frame.add(panStudentBut);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                       
        frame.setVisible(true);
    }
    
    
    public static void main(String[] args) {    
        SwingTutorial sw = new SwingTutorial();
        sw.createGUI();
    }

}

Solution

  • You appear to know and like to use GridLayout, but just as not carpentry task requires use of a hammer, sometimes, and often, you will want to use different layouts, and experiment with them.

    For example, consider

    For example:

    import java.awt.BorderLayout;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    
    import javax.swing.*;
    
    public class SwingTut2 extends JPanel {
        private static final int TF_COLS = 20;
        private static final String[] COURSES = { "Math", "Science", "History", "English", "Art", "Music", "PE",
                "Computer Science", "Biology", "Chemistry" };
        private static final int COURSE_LIST_ROW_COUNT = 5;
        private JTextField studentFirstName = new JTextField(TF_COLS);
        private JTextField studentLastName = new JTextField(TF_COLS);
        private JTextField studentEmail = new JTextField(TF_COLS);
        private JList<String> coursesList = new JList<>(COURSES);
        private JButton addStudentButton = new JButton("Add Student");
    
        public SwingTut2() {
            JPanel studentPanel = new JPanel(new GridBagLayout());
            studentPanel.setBorder(BorderFactory.createTitledBorder("Add Student"));
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.anchor = GridBagConstraints.WEST;
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1.0;
            gbc.weighty = 1.0;
            gbc.insets = new Insets(5, 5, 5, 5);
            gbc.fill = GridBagConstraints.HORIZONTAL;
            studentPanel.add(new JLabel("First Name:"), gbc);
            gbc.gridy++;
            studentPanel.add(new JLabel("Last Name:"), gbc);
            gbc.gridy++;
            studentPanel.add(new JLabel("Email:"), gbc);
            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.weightx = 2.0;
            studentPanel.add(studentFirstName, gbc);
            gbc.gridy++;
            studentPanel.add(studentLastName, gbc);
            gbc.gridy++;
            studentPanel.add(studentEmail, gbc);
    
            coursesList.setVisibleRowCount(COURSE_LIST_ROW_COUNT);
            JPanel coursePanel = new JPanel(new BorderLayout(5, 5));
            coursePanel.setBorder(BorderFactory.createTitledBorder("Courses"));
            coursePanel.add(new JScrollPane(coursesList), BorderLayout.CENTER);
    
            JPanel btnPanel = new JPanel();
            btnPanel.add(addStudentButton);
    
            setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
            setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            add(studentPanel);
            add(coursePanel);
            add(btnPanel);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> {
                SwingTut2 mainPanel = new SwingTut2();
                JFrame frame = new JFrame("SwingTut2");
                frame.add(mainPanel);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new SwingTut2());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            });
        }
    }
    

    creates:
    enter image description here

    Note that a GridBagLayout is a bit more complicate to use than a GridLayout since you have to become familiar with the GridBagConstraints class, since these objects need to be created, set correctly, and added to the container with the component that is being added. For example, in my code above, I have

    First create a studentPanel JPanel and give it a GridBagLayout and a border:

    JPanel studentPanel = new JPanel(new GridBagLayout());
    studentPanel.setBorder(BorderFactory.createTitledBorder("Add Student"));
    

    Create a GridBagConstraints object and set its key fields:

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.WEST; // components align to the left
    gbc.gridx = 0;   // starting at upper left location
    gbc.gridy = 0;
    gbc.weightx = 1.0;  // allow components to stretch in both directions
    gbc.weighty = 1.0;
    gbc.insets = new Insets(5, 5, 5, 5); // add some padding around components
    gbc.fill = GridBagConstraints.HORIZONTAL;  // let components fully fill the horizontal dimension
    

    // then add a component using this gbc constraint

    studentPanel.add(new JLabel("First Name:"), gbc);
    

    I then update the gbc object to meet the requirements for the next components being added:

    gbc.gridy++;
    studentPanel.add(new JLabel("Last Name:"), gbc);
    gbc.gridy++;
    studentPanel.add(new JLabel("Email:"), gbc);
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.weightx = 2.0;
    studentPanel.add(studentFirstName, gbc);
    // ....