4

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 :)

cubic lettuce
  • 5,116
  • 2
  • 14
  • 23
  • Could you specify how you want it to look? I mean to say `dockW1` should be at top utilising the whole width then `dockW2` followed by `dockW3` and rest to be to be filled up by `center`? – Blip May 20 '15 at 13:39
  • `dockW1`, `dockW2` and `dockW3` together should cover exactly the same west docking area as before, but aligned vertically in it. – cubic lettuce May 20 '15 at 14:01
  • I have edited my answer to accommodate what you have asked for. Kindly look at my answer below. – Blip May 20 '15 at 14:08

2 Answers2

2

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");
Blip
  • 2,711
  • 3
  • 17
  • 42
  • Thank you, (with `"cell 0 0"`, `"cell 0 1"`, `"cell 0 2"`) this fixes my above toy example. But the implicit reason for me to use the docking feature is that it allows to do any complex layout in the center area, independently of the docking area. When using cells at the top-level, I have to keep these first-column-cells manually in sync with the complex layout inside the center area - or I have to use a container as center, which is not better than using a container as side-panel. – cubic lettuce May 20 '15 at 14:33
  • @cubiclettuce why don't you have all the complex layout done by `MigLayout`. For me I feel it is the best layout giving all kinds of flexibilities and the best part is auto resizing of components when the container resizes. – Blip May 20 '15 at 15:12
  • The solution with the additional panel is what I meant by "... only by using an extra sidepanel-component" in my OP :) This is the standard way in Swing's BorderLayout. Indeed it is also possible this way in MigLayout, but I wonder whether the often cited feature of "multiple components per dock" is really that limited to being not configurable. For example I wonder whether layout parameters for the docks could be passed in the MigLayout's constructor, e.g., by something like `"fill, dock west {...params...}, dock east {...params...}"` – cubic lettuce May 20 '15 at 17:24
0

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.

DangeMask
  • 491
  • 3
  • 19