Arch1tect
Arch1tect

Reputation: 4281

Set two Jpanels side by side, no change when window is smaller

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));

Upvotes: 0

Views: 600

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

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

Upvotes: 1

Related Questions