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

Grammin picture Grammin · Jun 24, 2011 · Viewed 67.6k times · Source

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

Answer

Matthew Cox picture Matthew Cox · Jun 24, 2011

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.