0

I'm trying to place two JPanels onto a JFrame using GridBagLayout. The first JPanel uses gridLayout to create 35 columns and 27 rows of JButtons, and should have a width of 3/4 of the JFrame. This panel needs to fill the vertical space available to it. The second JPanel also uses gridLayout and should fill the last 1/4 of the main JFrame.

Unfortunately, my first JPanel (sPan) isn't even fitting properly on the screen, and is forcing the whole second JPanel (cPan) off of the screen. How can I constrain these values to take up only their allowed proportion on the screen without them moving each other around?

If I use a blank JPanel with a background colour with my code, everything works out perfectly fine:

Blank JPanel proportions [1]

However, when I use my JPanel consisting of JButtons, the proportions get completely messed up:

JButtons JPanel proportions [2]

I speculate that when I instantiate my sPan object, it's sizing each button to accommodate the size of the whole JFrame. Then when I instantiate the cPan object and try to place it next to the sPan object, the buttons aren't resizing themselves to accommodate the additional panel in the main JFrame. Does anybody know how I can fix this?

I have to use GridBagLayout for this assignment, so using normal gridLayout isn't an option. Any tips regarding what's happening would be appreciated.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
  • The first thing that jumps about is `sPan.setPreferredSize(new Dimension(630,481));` – MadProgrammer Mar 16 '14 at 22:56
  • Do I not need to set a preferredSize for the panel, since each button on the panel's width & height is determined by how large I want the panel to be? – Christian Abbott Mar 16 '14 at 23:05
  • 1
    [Don't use `setPreferredSize()` when you really mean to override `getPreferredSize()`](http://stackoverflow.com/q/7229226/230513). – trashgod Mar 16 '14 at 23:07
  • Okay, I've recoded it, but I'm still having the same issue. I've edited my question to be a bit more specific. Any ideas as to what's going on? – Christian Abbott Mar 17 '14 at 00:24

1 Answers1

1

Could you simply deal with two columns? The first one taking 5/6 of the available width and the second one taking the 1/6th remaining?

You could try the following:

final JPanel sPan = new JPanel();
sPan.setBackground(Color.RED);
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 5 / 6f;  // change this value...
constraints.weighty = 1.00;
add(sPan, constraints);

final JPanel cPan = new JPanel();
cPan.setBackground(Color.BLUE);
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 1 / 6f;  // ... and this one.
constraints.weighty = 1.00;
add(cPan, constraints);

Please note: I replaced your JPanels by some empty JPanels with a background color, so even like this you can see what's going on.

ccjmne
  • 9,333
  • 3
  • 47
  • 62
  • Although this works correctly when I use basic JPanels with just background colour, it doesn't seem to work when I replace these JPanels with my GridLayout JPanels. I run in to the same problem where my second JPanel has been pushed off the screen by the first one. I assume this is based on the first JPanels sizing of each button (which is based on the size of the window). – Christian Abbott Mar 16 '14 at 23:38