40

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

Grammin
  • 11,808
  • 22
  • 80
  • 138

1 Answers1

25

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.

Matthew Cox
  • 13,566
  • 9
  • 54
  • 72
  • 1
    I've used GridBag before but I was hoping there was just an adjust_column(column_num) kinda function for gridlayout – Grammin Jun 24 '11 at 17:32
  • 1
    @Grammin Bear in mind that I haven't kept up with the java API changes for the last two years. Having said that, I have been down the same road you were headed. As far as I know, the sizes are uniform in a standard GridLayout. Thus, the addition of the GridBadLayout which allows varying sizes of column and row elements – Matthew Cox Jun 24 '11 at 17:35
  • 24
    The problem is that for any beginner GridBagLayout is nothing but endless pain. If you had at least mentioned *how to do it with GridBagLayout*. this answer would suddenly be very helpful. – Tomáš Zato Mar 17 '15 at 12:54
  • 7
    Why would I reinvent the documentation though? There are tons of examples already for usage. – Matthew Cox Mar 27 '15 at 20:09
  • I also had not been using gridlayout for a while and was hoping not to need to use gridbag. – Andy McRae Mar 16 '21 at 12:29