Evgenij Reznik
Evgenij Reznik

Reputation: 18594

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?

Upvotes: 8

Views: 20042

Answers (3)

renderbox
renderbox

Reputation: 1655

The above answer is close. You need to add widgets to the Panel or Frame and not the Layout itself. The Layout just tells the panels/frames how to organize the widgets added to it.

panel.setLayout(new GridLayout(1,2));
panel.add(<your panel widget>);

Upvotes: 0

Tasawer Khan
Tasawer Khan

Reputation: 6148

You can use 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);

You can now set widths of grid1 one and grid2.

Upvotes: 1

Mayank Pandya
Mayank Pandya

Reputation: 1623

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.

Upvotes: 5

Related Questions