Changing width of GridLayout

user1170330 picture user1170330 · Apr 26, 2012 · Viewed 17.2k times · Source

I use the GridLayout for my JPanel and was wondering, if it's possible to change the width of the columns:

GridLayout grid = new GridLayout(5,2);
panel.setLayout(grid);

Is there a simple way?

Answer

Mayank Pandya picture Mayank Pandya · Apr 26, 2012

Check the GridLayout api to see that the grid is made so all cells are the same size. If you want a component to be shown at it's preferred size, like (15,15), add it to a JPanel and add the JPanel to the GridLayout. The JPanel will expand to fill the gird cell and allow the component to be shown at it's preferred size.

For a row of different size you'll have to try something else. Nested layouts would be a good starting place...

You could always use the dreaded GridBagLayout.