Change GridLayout Width

I use GridLayout for my JPanel and wondered if it is possible to change the width of the columns:

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

Is there an easy way?

+8
java layout swing grid-layout
source share
2 answers

You can use a separate grid layout for each column.

 GridLayout grid = new GridLayout(1,2); GridLayout grid1 = new GridLayout(5,1); GridLayout grid2 = new GridLayout(5,1); grid.add(grid1); grid.add(grid2); 

Now you can set the width of grid 1 and grid 2.

+4
source share

Check the GridLayout api to see that the grid is made so that all cells are the same size. If you want it to show the preferred size, for example (15,15), add it to JPanel and add JPanel to GridLayout. JPanel will expand to fill the gird cell and allow the component to display its preferred size.

For a string of different sizes, you have to try something else. Nested layouts would be a good starting point ...

You can always use the scary GridBagLayout.

+5
source share

All Articles