Containers and Layouts - jMonkeyEngine-Contributions/Lemur GitHub Wiki

Containers and Layouts

The only unique thing about a Container over its Panel super-class is that it provides convenient support for GuiLayouts. Following are the descriptions of the built-in layouts:

BorderLayout

Border Layout

BorderLayout is pretty straight forward. With a Container with a BorderLayout you can specify a location for what you are adding: Position.North, Position.Center, etc... and it will place the component there. This is similar to Swing's BorderLayout.

Container c = new Container(new BorderLayout());
c.addChild(new Label("Top"), Position.North);
c.addChild(new Label("Bottom"), Position.South);
...and so on...

Javadoc:BorderLayout

SpringGridLayout

SpringGridLayout is a little more complicated. Each item is added into a grid cell and then the rows and columns are sized based on the items in the cells. How the space is divided across the rows or columns is specified with the FillMode for that axis.

Whether the layout is row-major or column-major is also specified on the constructor. By default, a SpringGridLayout is row-major. Meaning a cell is row, column. Flipping the axes will make it column, row. But these are only notional anyway as the axes are in three-space... so you can even make one in x, z and so on.

Anyway, there are short-cuts so that you don't always have to specify the rows or columns.

If we use a default SpringGridLayout that is row-major (first part of the cell coordinate is y axis) then the following code will build a grid explicitly:

Container c = new Container();  // spring grid layout by default depending on styling
c.addChild(new Label("Foo"), 0, 0);
c.addChild(new Label("Bar"), 1, 0);
c.addChild(new Label("Baz"), 2, 0);

Will put three labels in a column.

But... with short-cuts, that's the same as:

Container c = new Container();  // spring grid layout by default depending on styling
c.addChild(new Label("Foo"));
c.addChild(new Label("Bar"));
c.addChild(new Label("Baz"));

By default, if no constraints are specified then it will increment the row and reset the column.

This also means you can do stuff like:

Container c = new Container();  // spring grid layout by default depending on styling
c.addChild(new Label("Foo:"));
c.addChild(new Label("123"), 1);
c.addChild(new Label("Bar:"));
c.addChild(new Label("456"), 1);
c.addChild(new Label("Baz:"));
c.addChild(new Label("789"), 1);

to make a two-column panel. When only one number is provided it is considered to be the 'column'.

The above is the same as doing the following explicitly:

Container c = new Container();  // spring grid layout by default depending on styling
c.addChild(new Label("Foo:"), 0, 0);
c.addChild(new Label("123"), 0, 1);
c.addChild(new Label("Bar:"), 1, 0);
c.addChild(new Label("456"), 1, 1);
c.addChild(new Label("Baz:"), 2, 0);
c.addChild(new Label("789"), 2, 1);

The FillModes try to cover most of the sizing that one might want but eventually this class will be refactored to support a FillModel to allow the user to fully customized how the rows or columns are stretched. Then FillModes just become a set of default models. I haven't gotten there yet but the existing FillModes cover 95% of use-cases I'd say. The others can sometimes be faked with nested panels... though I've also subclassed SpringGridLayout before, too.

BoxLayout

BoxLayout you can probably just ignore.

Javadoc:BoxLayout