Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make my columns different sizes using GridLayout in swing?

I'm using a GridLayout and my code is as follows:

int changingVar = 1;  JPanel panel = new JPanel(new GridLayout(changingVar, 2)); panel.add(new JButton("BUTTON1")); panel.add(new JButton("BUTTON2")); 

This looks like:

___________________________________________ | [      BUTTON1     ] [     BUTTON2     ] | ___________________________________________ 

which is two evenly sized columns. I would like to make it like this:

___________________________________________ | [          BUTTON1         ] [ BUTTON2 ] | ___________________________________________ 

in which one column takes up more of the panel space then the other. How do I do this with gridlayout? I am not oppose to using another layout as long as I can have a varying amount of rows and columns that are two different sizes.

Thanks

like image 493
Grammin Avatar asked Jun 24 '11 17:06

Grammin


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.

What is the difference between GridLayout and GridBagLayout?

A GridLayout puts all the components in a rectangular grid and is divided into equal-sized rectangles and each component is placed inside a rectangle whereas GridBagLayout is a flexible layout manager that aligns the components vertically and horizontally without requiring that the components be of the same size.

How do I add components to GridLayout?

To add Components to a GridLayout You do not (can not) use the row and column to tell where to add the components -- add them in starting at the top left and going across the row first.

Which is the correct constructor for GridLayout?

Class constructors S.N. Creates a grid layout with a default of one column per component, in a single row. Creates a grid layout with the specified number of rows and columns.


1 Answers

If you want this effect then you need to utilize the GridBagLayout.

http://download.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

Have fun with that one =P

EDIT:

You can work around the problem by employing a mixture of FlowLayout and GridLayout to get a similar effect. However, this solution will become extremely tedious and messy as your layout complexities become bigger.

like image 52
Matthew Cox Avatar answered Oct 01 '22 13:10

Matthew Cox