GUI autoresize problem

Hi, I have a GUI, and whenever I add in 3 panels to the frame, the middle panel would always become squished.

The frame has a borderlayout, and I attach a panel to the frame. The panel attached has a gridlayout that contains a bunch of JLabels (puzzleGrids() returns a JPanel):

frame.getContentPane().setLayOut(new BorderLayout());

frame.getContentPane().add(puzzleGrids(), BorderLayout.CENTER);

frame.setSize(500,500);

frame.setResizable(false);

If you run the GUI at this point, you'll see a bunch of JLabels nicely displayed. Then I go back to the code and I add in three more panels to the frame. They are added BorderLayout.NORTH, BorderLayout.EAST, and BorderLayout.SOUTH. Now when you run the GUI, the center panel will become squished. So the JLabels now look smaller and more rectangular. The grids for the center panel became smaller!

I've added in a feature so that the user can hide/show the three side panels at will. If the user hides all the panels, then the center panel will go back to looking normal. However, if any of the panels are visible, then the center panel will become distorted.

I would like the center panel to stay the same size at all times. So originally, it is 500x500 (look at the above code where I set the size). How do I fix this so that the center panel will stay the same size, but the frame itself becomes larger so as to accommodate the 3 side panels? I have already tried to reset the size of the frame, but that only increased the frame. The side panels stayed the same size; they did not resize to the new frame size.

[1619 byte] By [xboxdisca] at [2007-11-27 3:37:45]
# 1

In the future, Swing related questions should be posted in the Swing forum.

> whenever I add in 3 panels to the frame, the middle panel would always become squished

Don't use the setSize(....) method. Use the pack() method.

The component added to the Center get the remaining space after all the other components get all the space they require. So if you specify the wrong size the center panel will be "squished" or "too big".

camickra at 2007-7-12 8:41:02 > top of Java-index,Java Essentials,Java Programming...
# 2
But the problem with pack() is that it packs everything tightly. Packing all that makes the center panel even more distorted. Rather than the frame being at size 500x500, pack shrinks down the frame to something less than 500x500.
xboxdisca at 2007-7-12 8:41:02 > top of Java-index,Java Essentials,Java Programming...
# 3

pack() sizes components to their preferred size. If you believe the center panel is too small then the preferred size of the components added to the middle is not what you want.

Use the setPreferredSize() method on the component you are adding to the center.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.

camickra at 2007-7-12 8:41:02 > top of Java-index,Java Essentials,Java Programming...