Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridLayout wrong number of columns

I'm trying to create panel with a GridLayout(7,2)

membersPanel = new JPanel(new GridLayout(7,2));

However, when I add the components (Labels, Combobox, textfields, etc) the components show up in 3 columns, like this:

Image

I've tryed changing the number of columns to 1 or even 0, but the panel stays the same. What can I do?

EDIT:

Here's more code:

 p1 = new JPanel();
membersPanel = new JPanel(new GridLayout(7,0));

resourcesLabel = new JLabel("Resources");
    membersPanel.add(resourcesLabel);       

    emptyLabel5 = new JLabel("");
    membersPanel.add(emptyLabel5);

    emptyLabel6 = new JLabel("");
    membersPanel.add(emptyLabel6);

    comboBoxResource = new JComboBox(configs.XMLreaderDOM4J.readResourceID());
    membersPanel.add(comboBoxResource);

    slider1 = new SliderWithTextField(1,10);
    textSli1 = new TextFieldFromSlider(this, slider1); 
    slider1.setTextField(textSli1);

    slider1.setValue(1);
    membersPanel.add(slider1);

    membersPanel.add(textSli1);

    emptyLabel2 = new JLabel();
    membersPanel.add(emptyLabel2);

    addButton1 = new JButton("Add");
    addButton1.addActionListener(new TrataEvento());
    membersPanel.add(addButton1);



    agregator1Label = new JLabel("Agretagor1");
    membersPanel.add(agregator1Label); 

    comboBoxAgregator1 = new JComboBox(configs.XMLreaderDOM4J.readAgregator1ID());
    membersPanel.add(comboBoxAgregator1);

    slider2 = new SliderWithTextField(1,10);
    textSli2 = new TextFieldFromSlider(this, slider2); 
    slider2.setTextField(textSli2);

    slider2.setValue(1);
    membersPanel.add(slider2);

    membersPanel.add(textSli2);
    addButton2 = new JButton("Add");
    addButton2.addActionListener(new TrataEvento());
    membersPanel.add(addButton2);

    emptyLabel3 = new JLabel();
    membersPanel.add(emptyLabel3);


    agregator0Label = new JLabel("Agregator0");
    membersPanel.add(agregator0Label);     

    comboBoxAgregator0 = new JComboBox(configs.XMLreaderDOM4J.readAgregator0ID());
    membersPanel.add(comboBoxAgregator0);

    slider3 = new SliderWithTextField(1,10);
    textSli3 = new TextFieldFromSlider(this, slider3); 
    slider3.setTextField(textSli3);

    slider3.setValue(1);
    membersPanel.add(slider3);

    membersPanel.add(textSli3);

    addButton3 = new JButton("Add");
    addButton3.addActionListener(new TrataEvento());
    membersPanel.add(addButton3);

    emptyLabel4 = new JLabel();
    membersPanel.add(emptyLabel4);

p1.add(membersPanel); 
like image 416
SaintLike Avatar asked Dec 21 '22 04:12

SaintLike


1 Answers

Swing adjusts the number of columns used for GridLayout if the number of components added exceeds the original number specified. Use 0 to specify an adjustable number of rows:

membersPanel = new JPanel(new GridLayout(0, 2));

This will make any future refactoring easier.

like image 121
Reimeus Avatar answered Jan 02 '23 13:01

Reimeus