0

I recently decided to start using GridLayout because FlowLayout seems somewhat amateur. However, I need help. The parameters when creating the GridLayout are (rows,columns,row space,column space). I have a variable for the row amount and 4 for the column amount, but when I try to add a JButton after everything else, there are 5 columns. Here is my code:

byte i = 0;
    while(i < main.componentNum)
    {
        comp[i] = new JLabel("component #" + (i+1));
        box[i] = new JComboBox();
        field[i] = new JTextField(5);
        edit[i] = new JButton("edit");

        comp[i].setBackground(Color.WHITE);
        box[i].setBackground(Color.WHITE);
        field[i].setBackground(Color.WHITE);
        edit[i].setBackground(Color.WHITE);

        add(comp[i]);
        add(box[i]);
        add(field[i]);
        add(edit[i]);

        i++;
    }

When I run the above code, I get four columns and it works fine. But when I add a button to the end, I get five. Can anyone tell me how to give one button an entire row?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Squirvin
  • 129
  • 5

1 Answers1

4

From the Java Docs

One, but not both, of rows and cols can be zero, which means that any number of objects can be placed in a row or in a column.

Now, without your actual code the sets up the GridLayout, it's difficult to say, but, if your after maintaining only 4 columns, I would create a GridLayout as follows, new GridLayout(0, 4)

If you want something more flexible, look into GridBagLayout

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 2
    +1 for explaining a frequently misunderstood feature. Here's a minimal [example](http://stackoverflow.com/a/5751044/230513). – trashgod Nov 10 '12 at 20:34
  • Yes, although I figured it out before getting a reply, thank you very much. +1 – Squirvin Nov 10 '12 at 22:56