Intelligent Vertical Layout

I was looking for the following behavior in a JPanel (Swing) layout: basically it will compose the components vertically, one below each other.

When the components cannot fit vertically in the container, it must add the next to a new line. This will continue dynamically, adding new lines as needed.

It would seem like this by adding 3 tags:

+--------------------------+ | label1 | | label2 | | label3 | +--------------------------+ 

After adding: 2 more shortcuts:

 +--------------------------+ | label1 label4 | | label2 label5 | | label3 | +--------------------------+ 

Finally, after adding two more labels, it will look like this:

 +--------------------------+ | label1 label4 label7 | | label2 label5 | | label3 label6 | +--------------------------+ 

Is it possible to achieve this behavior using one of the current layouts?

Should I create it myself?

How would you decide this decision?

+2
source share
2 answers

Yes it is possible. Try using MigLayout .

Here is a code snippet that illustrates usage:

 JPanel panel = new JPanel(new MigLayout("fill, flowY, wrap 4)); panel.add(new JLabel("row 1, column 1")); panel.add(new JLabel("row 2, column 1")); panel.add(new JLabel("row 3, column 1")); panel.add(new JLabel("row 1, column 2")); // etc. 
+1
source

Ok, solution found. Thanks to Boris using MigLayout :

 LC lc_Y = (new LC()).fill().flowY(); lc_Y.setWrapAfter(ITEMS_PER_COLUMN); JPanel panel = new JPanel(new MigLayout(lc_Y)); 

Now it would be ideal if there was no need to indicate the number of elements per column. I mean, Layout will try to vertically fill the container with as many elements as possible on the same line.

0
source

All Articles