Xander
This is a quick start to MiG Layout. For further information look at www.migcomponents.com as it will be
updated with links to the new and relevant information.
To start using MiG Layout all you need to do is to download the miglayout.jar and include it in your project or
classpath. There are different versions for Swing and SWT, but they work exactly thesame. There is also a
version for JSE 1.5 and one for 1.4 where the latter has varargs support in the API.
Adding Components to the Grid
Adding components to a Container is as simple as writing text and follows the same basic principle. If you
just add them they will end up on the same row. When you want to change to next row you specify the
constraint "wrap" and the next component will beon the next row. For example:
panel.add(comp1)
panel.add(comp2)
panel.add(comp3, "wrap")
panel.add(comp4)
comp1
comp2
// Wrap to next row
comp3
comp4
The grid can also be set to auto wrap at a specific column index by specifying that in the layout constraint
when creating the layout manager. Next shows how to create the same grid without having to specify the
"wrap" when addingcomp3. It means that the grid should auto wrap after column 3 and there will thus not be
a fourth column.
MigLayout layout = new MigLayout("wrap 3");
From v2.5 the next row's gap can be set directly after the wrap keyword. E.g:
panel.add(comp3, "wrap 15")
will make the row gap 15 pixels high. You can even make the gap "pushing" by specifying for instance "wrap
push".
Merging and SplittingCells
It is equally easy to split or span cells. Here is how to create the next grid.
panel.add(comp1)
panel.add(comp2, "span 2") // The component will span two cells.
panel.add(comp3, "wrap")
// Wrap to next row
panel.add(comp4, "span")
// Span without "count" means span whole row.
comp1
comp2
comp3
comp4
Span optionally takes two indexes, x and y. This means that you can spancells like this:
© 2009 MiG InfoCom AB
panel.add(comp1);
panel.add(comp2, "span 2 2");
panel.add(comp3, "wrap");
panel.add(comp4);
panel.add(comp5, "wrap");
panel.add(comp6);
panel.add(comp7);
comp1
// The component will span 2x2 cells.
// Wrap to next row
// Note that it "jumps over" the occupied cells.
comp3
comp2
comp4
comp6
comp5
comp7
It is equally easy andintuitive to split cells.
panel.add(comp1);
panel.add(comp2, "split 2");
panel.add(comp3);
panel.add(comp4, "wrap");
panel.add(comp5);
comp1
comp2 comp3
// Split the cell in two
// Will be in same cell as previous
// Wrap to next row
comp4
comp5
It is of course possible to both span and split cells at the same time. You can for instance span three cells
and split thatthree-cell-wide cell into two.
Using Absolute Cell Coordinates
If you don't want to use the "flow" way to put components into grid positions you can instead use absolute
coordinates. For instance:
panel.add(comp1,
panel.add(comp2,
panel.add(comp3,
panel.add(comp4,
"cell
"cell
"cell
"cell
0
1
2
0
0"); // "cell column row"
0");
0");
1");
Would produce the same grid as the firstexample at the top.
comp1
comp2
comp3
comp4
You can also use the absolute cell way to span and split cells. If a component is put in a cell that already has
a component the cell will be split and both cells will end up in the same cell, sharing its space. To make the
same grid as the second example above you do like this:
© 2009 MiG InfoCom AB
panel.add(comp1,
panel.add(comp2,panel.add(comp3,
panel.add(comp4,
comp1
"cell
"cell
"cell
"cell
0
1
3
0
0");
0 2 1");
0");
1 4 1");
comp2
// "cell column row width height"
comp3
comp4
Specifying Gaps
Generally gaps are added where they make sense and this is performed on a platform by platform basis. For
instance Mac OS X will have bigger gaps than Windows or Linux in general. There are...
Regístrate para leer el documento completo.