0

I have a bottomPanel, and I want to add two panels side by side into this panel. They are bottomLeft and bottomRight panel. So I'm thinking if I set the minimum size of outter panel larger than the width when they are side by side, when I make the window smaller, the two panels should maintain side by side. But bottomRight always goes under bottomLeft. Below is the code and I use flowLayout for bottomPanel.

bottomPanel.add(bottomPanelRight);
bottomPanel.add(bottomPanelLeft);
bottomPanel.setMinimumSize(new Dimension(600, 600));
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Arch1tect
  • 4,128
  • 10
  • 48
  • 69
  • 2
    1) Seems this needs a `GridLayout`. 2) For better help sooner, post an [SSCCE](http://sscce.org/). 3) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Nov 10 '13 at 00:24
  • What do you want to happen to the two panels. Do they shrink or maintain their original size? Without knowing the exact requirement we can't give a specific. You can also look at a BoxLayout or a GridBagLayout, but FlowLayout will NOT work. Read the Swing tutorial on [Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) for more information and examples. – camickr Nov 10 '13 at 00:30
  • @AndrewThompson I can't use GridLayout because bottomLeft and bottomRight are different size... – Arch1tect Nov 10 '13 at 00:55
  • @camickr Maintain same size.. I'm reading your recommendation now... – Arch1tect Nov 10 '13 at 00:56
  • Consider telling us all the important details about your requirements and your problem. – Hovercraft Full Of Eels Nov 10 '13 at 00:56
  • So I just want bottomLeft and bottomRight panel to always be side by side even when I resize and make the window smaller. Because you know when you make it smaller, the one on the right just go under the one on the left. This is for FlowLayout, maybe I should use something else – Arch1tect Nov 10 '13 at 00:59
  • Yes, use something else. Consider adding both of the bottom JPanels to a GridBagLayout-JPanel and then add the larger container/JPanel to your GUI in BorderLayout.SOUTH position. But most importantly, read up on how to use the layout managers. – Hovercraft Full Of Eels Nov 10 '13 at 01:12

1 Answers1

1

The immediate issue seems to be the fact the the default layout of a JPanel is FlowLayout (since I can't see any code changing the layout)

You Could

Try using a GridLayout. This will ensure that both the components are given equal space within the container, meaning that they will change size as the parent container changes size.

You Could

Use a GridBagLayout, which will provide you greater ability to determine how each component is laid out within their given cells.

GridBagLayout will, if not told to do otherwise, use the preferred size of the components. If there is not enough space to honour the preferred size, it will use the components minimum size instead

Take a look at Laying Out Components Within a Container for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366