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?
While you can't make components span multiple rows with GridLayout, you can resize them all uniformly. Put your GridLayout on its own JPanel, and then you can use panel. setSize(x,y) to change the panel size and thus increase or decrease the size of the cells.
Constructors of GridLayout classGridLayout(int rows, int columns): creates a grid layout with the given rows and columns but no gaps between the components. GridLayout(int rows, int columns, int hgap, int vgap): creates a grid layout with the given rows and columns along with given horizontal and vertical gaps.
Creates a grid layout with the specified number of rows and columns. All components in the layout are given equal size. 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With