hamena314
hamena314

Reputation: 3109

How to stack components inside a vertical cell in the MigLayout?

I'm trying to understand the MigLayout, therefore I want to create a table with different panels inside.

At the moment it's looking like this: no Flow in y direction

The cell 2 contains 3 labels, which should stack. Therefore I tried to give cell 2 it's own layout with center2.setLayout(new MigLayout("flowy"));, but then the other components get cluttered:

Flow y, but cluttered

So is there a way, to stack the 3 labels in cell 2 vertically?

[EDIT]: center1 and center2 should have the same height!

My example class PageThree.java:

public class PageThree extends JPanel{

    public PageThree() {
        setLayout(new MigLayout());
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new MigLayout("width 100%"));

        JPanel topHeading = new JPanel();
        JPanel westAreas = new JPanel();
        JPanel center1 = new JPanel();
        JPanel center2 = new JPanel();

        // If I give center2 its own layout with flowy, 
        // the 3 labels are stacked vertically, 
        // but the other components get cluttered

        // center2.setLayout(new MigLayout("flowy"));
        JPanel center3 = new JPanel();

        center1.add(new JLabel("center1"));

        center2.add(new JLabel("center21"));
        center2.add(new JLabel("center22"));
        center2.add(new JLabel("center23"));

        center3.add(new JLabel("center3"));
        topHeading.add(new JLabel("topHeading1"));
        topHeading.add(new JLabel("topHeading2"));
        topHeading.add(new JLabel("topHeading3"));
        westAreas.add(new JLabel("westAreas"));

        contentPanel.add(center1, "width 40%");
        contentPanel.add(center2, "width 60%, wrap");
        contentPanel.add(center3, "width 50%");
        contentPanel.add(topHeading, "width 100%, dock north, split 3");
        contentPanel.add(westAreas, "dock west");
    }
}

Upvotes: 2

Views: 372

Answers (1)

Matt Hubbard
Matt Hubbard

Reputation: 101

To answer your question keep the "flowy" line and put "grow" into

contentPanel.add(center1, "width 40%");

to become

contentPanel.add(center1, "width 40%, grow");

The "grow" constraint tells the component to grow in weight compared to other components in the cell. If it is the only component in the cell, it will fill the cell (which is what I think you want). To understand it better use "debug" in the MigLayout constructor. This gives you borders around the cells (red) and the components in the cells (blue).

contentPanel.setLayout(new MigLayout("width 100%, debug"));

Upvotes: 3

Related Questions