MigLayout: How to * vertically * align multiple components inside a dock?

MigLayout supports adding multiple components to the dock. I want to add multiple components to the western dock from top to bottom. However, it seems like MigLayout can control the horizontal layout inside the dock. I have tried many options (like wrap, growth, flow) with no success.

So, is there any option to wrap or set vertical flow inside the dock? Or is this not possible with MigLayout, but only with an additional sidebar?

Here's an example of an unwanted horizontal layout inside a western dock:

example of unwanted horizontal layout inside west dock

How to get red, green, blue components below each other? Here is the code:

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

public class MigTest extends JFrame {

    MigTest() {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(800, 600);

        setLayout(new MigLayout("fill"));

        JTextField dockW1 = new JTextField("West 1"); dockW1.setBackground(Color.red);
        JTextField dockW2 = new JTextField("West 2"); dockW2.setBackground(Color.green);
        JTextField dockW3 = new JTextField("West 3"); dockW3.setBackground(Color.blue);
        JTextField center = new JTextField("Center"); center.setBackground(Color.lightGray);

        add(center, "grow");

        // HOW TO LAYOUT THESE COMPONENTS VERTICALLY INSIDE WEST DOCK ?
        add(dockW1, "dock west, wrap, growy, flowy");
        add(dockW2, "dock west, wrap, growy, flowy");
        add(dockW3, "dock west, wrap, growy, flowy");

        setVisible(true);
    }

    public static void main(String[] args) {
        new MigTest();
    }
}

      

[edit] . Note: I do not want to put dockW1

, dockW2

, dockW3

and center

in the same grid as the plan to use complex located in the central region, regardless of the side of the field, because of what has been created docking function :)

+3


source to share


2 answers


IMHO the sidebar is a simpler option with the same result.



You can also try using cell coordinates as written on page 2 in the Quick Start Guide .

0


source


My first suggestion is to change the constructor MigLayout

to

new MigLayout("fill","[][grow]","[][][]")

      

Then change your add operator to:

 add(center, "cell 1 0 1 3, grow");
 add(dockW1, "cell 0 0");
 add(dockW2, "cell 0 1");
 add(dockW3, "cell 0 2");

      



Edit

After you have edited the question, I suggest you create a new JPanel

say object dockWest

and add components dockW1

, dockW2

and dockW3

in dockWest

and finally attach dockWest

to the west of the current one JFrame

, like:

JPanel dockWest = new JPanel();
dockWest.setLayout(new MigLayout("fill", "[]", "[grow][grow][grow]");
dockWest.add(dockW1, "cell 0 0");
dockWest.add(dockW2, "cell 0 1");
dockWest.add(dockW3, "cell 0 2");

add(dockWest, "dock west, growy");

      

0


source







All Articles