Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing width of GridLayout

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?

like image 643
Evgenij Reznik Avatar asked Apr 26 '12 11:04

Evgenij Reznik


People also ask

How do I change GridLayout size?

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.

Which is the correct constructor of GridLayout?

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.

How does GridLayout work in Java?

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.


1 Answers

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.

like image 141
Mayank Pandya Avatar answered Oct 19 '22 19:10

Mayank Pandya