Pergunta

I'm new to MigLayout. I'm attempting to create a layout similar to this:

enter image description here

There is a row of equally spaced buttons, followed by a row with a list that spans all of the columns and grows to fill the available vertical space, followed by a final row with a few more controls.

I can get the first two rows without much difficulty.

However, when I add the content of the final row, MigLayout (rightfully) attempts to preserve the columns of the first two rows. I'm left with something like this:

enter image description here

The label and spinner in the last row extend the width of the columns and I'm left with uneven gaps in the top row.

Is there some way to tell MigLayout that I want to forget about the rows/columns established thus far and to "start fresh," or is the solution here to create a nested layout?

Here's a full example panel.

public class TestPanel extends JPanel {

    JButton button1 = new JButton("Button 1");
    JButton button2 = new JButton("Button 2");
    JButton button3 = new JButton("Button 3");
    JButton button4 = new JButton("Button 4");
    JButton button5 = new JButton("Button 5");

    JList list = new JList(new String[]{"some", "fake", "data"});

    JLabel label = new JLabel("this is my long label");
    JSpinner spinner = new JSpinner();
    JCheckBox checkbox = new JCheckBox("Check me");

    public TestPanel() {
        initComponents();
    }

    private void initComponents() {

        setLayout(new MigLayout());

        add(button1);
        add(button2);
        add(button3);
        add(button4);
        add(button5, "wrap");

        add(list, "span, growx, growy, wrap");

        // without these 3 lines, the row of buttons are equally spaced
        // adding the long label extends the width of the first column
        add(label);
        add(spinner);
        add(checkbox, "span, align right");
    }
}
Foi útil?

Solução

I was able to achieve the desired layout by merging and splitting cells.

setLayout(new MigLayout());
add(button1);
add(button2);
add(button3);
add(button4);
add(button5, "wrap");
add(list, "span, growx, growy, wrap");

// merge 4 cells then split the combined cell in half
// label goes in the first cell of the split
// spinner goes in the second cell of the split
add(label, "span 4, split 2);
add(spinner);
// check box goes in the 5th and final cell of the row (after the 4 merged cells)
add(checkBox, "align right");

Here's the result:

enter image description here

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top