cubic lettuce
cubic lettuce

Reputation: 6559

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

MigLayout supports adding multiple components to a dock. I want to add multiple components to the west dock, from top to bottom. However, it seems as if MigLayout can only manage a horizontal layout inside a dock. I tried many parameters (e.g., wrap, growy, flowy) without success.

So, is there any possibility to wrap or set a vertical flow inside a dock? Or is this not possible with MigLayout itself, but only by using an extra sidepanel-component?

Here an example of the unwanted horizontal layout inside the west dock:

example of unwanted horizontal layout inside west dock

How to get the 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 that I do not want to put dockW1, dockW2,dockW3, and center into a single grid, since I plan to apply a complex layout in the center area, independently of the side-area, which is the reason why the docking feature was invented :)

Upvotes: 4

Views: 1594

Answers (2)

Blip
Blip

Reputation: 3171

My first suggestion is to change the constructor of the MigLayout to

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

Then change your add statement 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 edited the question, I would suggest you to create a new JPanel object say dockWest and add the components dockW1, dockW2 and dockW3 to dockWest and finally dock the dockWest to the west of the current 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");

Upvotes: 2

DangeMask
DangeMask

Reputation: 551

IMHO the side-panel is easier option with the same result.

You can also try using cell coordinates as written on page 2 in Quick guide.

Upvotes: 0

Related Questions