ContinuousError
ContinuousError

Reputation: 125

A Flexible Layout manager that will change the layout according to resize

In my JPanel, I have 6 buttons laid out in a row (using FlowLayout as of now). The default size of the panel is enough to accommodate these buttons in one row. But, when the frame is resized it gets stuck at the size that is the sum of the minimum sizes of each button.

I need a layout manager that simply puts the buttons in a new row on re-sizing of the panel. I'm pretty new to Java Swing so I apologize in advance if this is a trivial question.

Upvotes: 2

Views: 581

Answers (3)

camickr
camickr

Reputation: 324207

FlowLayout does actually paint components on a new row, but the problem is that the preferred size of the panel doesn't change so in many cases you can't see the components (unless you happen to add the panel to the CENTER of a BorderLayout).

One solution is to use the Wrap Layout, which extends FlowLayout to recalculate the preferred size of the panel so that you see the buttons on a new row.

Upvotes: 0

Kojotak
Kojotak

Reputation: 2070

The flow layout is capable of your desired behavior (moving components into new row if they cannot fit). Check out the swing tutorial (run FlowLayoutDemo). You'll have to show us your source code to find out, whether there is some other constrain which prevents it.

Upvotes: 2

lance-java
lance-java

Reputation: 28099

MigLayout is by far the best layout manager I've ever used. Things that used to require nested containers and lots of hard to understand code can be done in a single container with simple to understand (and maintain) string constraints.

Upvotes: 3

Related Questions