开发者

Smart Vertical flow layout

I'm looking for the following behaviour in a JPanel Layout (Swing): basically it would arrange the components in a Vertical way, one bellow each other.

When the components can't fit vertically in the container, it should add the next one in a new row. This would continue dynamically, adding new rows as needed.

It would look likes this, after adding 3 labels:

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

After adding: 2 more labels:

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

Finally,开发者_运维技巧 after adding 2 more labels it would look like this:

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

Is this behaviour possible to achieve with one of the current layouts?

Should I create one myself?

How would you solve this solution?


Yes, it is possible. Try using MigLayout.

Here's a code snippet which illustrates the 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.


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 perfect if there was no need to specify the amount of items per column. I mean, the Layout would try to fill vertically the container with as many items in the same row as possible.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜