Search code examples
javaswingcomboboxgridbaglayout

Java Restraining width for JPanel with GridBagLayout


I wrote a program to generate PDF tables. To make it easier to manipulate, I also created a GUI to control the things. However, It really bothers me that on JFrame, this JComboBox takes a lot of space, making the right subPanel unnecessarily wide.

The reason for this is probably that there is a pretty long string in the comboBox.

I tried using "set preferred size" for the panel, but it didn't work. I then proceeded to use GridBagLayout to try to narrow that combo box. Is there any way to restrict all the buttons and the comboBox to a certain width? Or should I use a different layout?

It would be best if all components in that right panel can have the same width, just like what they are now.

Thank you!

enter image description here

here is my code:

JTable studentTable=new JTable(new StudentTableModel(bro));
            studentTable.setRowHeight(25);
            studentTable.setGridColor(java.awt.Color.BLACK);
            DefaultTableCellRenderer centerRenderer = new     
DefaultTableCellRenderer();
            centerRenderer.setHorizontalAlignment(SwingConstants.CENTER);

studentTable.getColumnModel().getColumn(2).setCellRenderer(centerRenderer);

studentTable.getColumnModel().getColumn(3).setCellRenderer(centerRenderer);

studentTable.getColumnModel().getColumn(5).setCellRenderer(centerRenderer);
        JPanel panelA1= new JPanel();
            panelA1.setOpaque(false);
            panelA1.setAlignmentX(Component.LEFT_ALIGNMENT);
            panelA1.setLayout(new GridBagLayout());

        JScrollPane scrollPaneA= new JScrollPane(studentTable);

        JButton buttonA1= new JButton("Activate All");

        JButton buttonA2= new JButton("Create Tables");

        JButton buttonA3= new JButton("Save File");

        JLabel labelA1= new JLabel("-Job Crews-");

        JButton buttonA4= new JButton("Deact Crew");

        JComboBox<Object> comboBoxA1=new JComboBox<Object>();
        comboBoxA1.addItem(new String("-N/A-"));
        for(JobCrew jj: crews)
        {
            comboBoxA1.addItem(jj);
        }
        comboBoxA1.setMaximumSize(new Dimension(100,100));

        GridBagConstraints c = new GridBagConstraints();
        {
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 0;
            panelA1.add(buttonA1,c);

            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 1;
            panelA1.add(buttonA2,c);

            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 2;
            panelA1.add(buttonA3,c);

            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 3;
            panelA1.add(labelA1,c);

            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 4;
            panelA1.add(comboBoxA1,c);

            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 5;
            panelA1.add(buttonA4,c);

        }

        JPanel panelA= new JPanel();
            panelA.setLayout(new BoxLayout(panelA,BoxLayout.X_AXIS));
            panelA.setOpaque(false);
            panelA.add(scrollPaneA); panelA.add(panelA1);
        //////
        /**********/
        tabs.addTab("Student info",null,panelA,"Organizes & displays student information");

Solution

  • Check out Combo Box Popup.

    enter image description here

    It will allow you to control the width of the combo box itself and the popup can be the full width.

    Or you can set a property so that a scrollbar will be displayed in the popup if required.