Squirvin
Squirvin

Reputation: 129

GridLayout columns

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?

Upvotes: 0

Views: 279

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

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

Upvotes: 4

Related Questions