PnutsLayout

The PnutsLayout is a general purpose geometry manager. It is more programmer-friendly than typical LayoutManagers but is as flexible as the GridBagLayout. Combined with the Hierarchical Layout, it allows layout definition similar to TABLE tag in HTML. Although PnutsLayout is a part of Pnuts, it can be used without Pnuts as an ordinary LayoutManager.

In PnutsLayout, components are added with properties. Parameter of constructor and add() method is a comma-separated list of "propertyName = value". Parameters of a constructor gives default value of properties for components.

e.g.:

setLayout(new PnutsLayout("cols = 3"));
add(button1, "padx = 20, pady = 20");
add(button2, "colspan = 2");
add(button3, "halign = left, valign = top");

Properties are:

propertydescriptiondefaultconstructoradd()
colsNumber of columns.1OK-
uniform
"x": if width of each column is same
"y": if height of each row is same
"xy": if both of them are same
""OK-
colspannumber of columns the component occupies1-OK
rowspannumber of rows the component occupies1-OK
padxexternal padding in x0OKOK
padyexternal padding in y0OKOK
ipadxinternal padding in x. The added component gets larger in x.0OKOK
ipadyinternal padding in y. The added component gets larger in y.0OKOK
halignalignment of x. One of "left", "right","center","fill"centerOKOK
valignalignment of y. One of "top", "bottom", "center", "fill"centerOKOK
expand
"x": expand the area of the component as the width of container changes.
"y": expand the area of the component as the height of container changes.
"xy": expand the area of the component as the size of container changes.
""OKOK

Number of Columns (cols)

setLayout(new PnutsLayout("cols=3"));
add(new Button("OK"));
add(new Button("Cancel"));
add(new Button("Help"));

In PnutsLayout, the number columns are always defined, and the layout of components is from left to right, top to bottom.

External Padding (padx, pady)

setLayout(new PnutsLayout("cols=3,padx=4,pady=10"));
add(new Button("OK"));
add(new Button("Cancel"));
add(new Button("Help"));

PnutsLayout layouts virtual bounding-boxes which includes a component. Each size of bounding-boxes is at least "preferredSize" of the corresponding component.

When an external padding is specified, bounding-box becomes larger as the number of pixels increases.

After layouting all components, the width of a bounding-box is the maximum width of the same column, and a height of the bounding-box is the maximum height of the same row.

Internal Padding (ipadx, ipady)

setLayout(new PnutsLayout("cols=3,ipadx=4,ipady=10"));
add(new Button("OK"));
add(new Button("Cancel"));
add(new Button("Help"));

The Size of a component is "preferredSize" by default. When an internal padding is specified, the component gets larger as the number of pixels increases.

Horizontal Alignment (halign)

setLayout(new PnutsLayout("halign=left"));
add(new Button("OK"));
add(new Button("Cancel Command"));
add(new Button("Help"));

Components are located in the middle of the bounding-box by default. When a property halign is specified, a component can be left-aligned, right-aligned, and filled in the bounding-box.

setLayout(new PnutsLayout("halign=left,pady=5,padx=5"));
add(new Button("OK"));
add(new Button("Cancel Command"));
add(new Button("Help"));

Fill

setLayout(new PnutsLayout("halign=fill"));
add(new Button("OK"));
add(new Button("Cancel Command"));
add(new Button("Help"));

Multi-rows component (rowspan)

setLayout(new PnutsLayout("cols=2,halign=fill"));
add(new Button("OK"), "rowspan=2,valign=fill");
add(new Button("Cancel"));
add(new Button("Help"));

A component can occupy multiple columns or rows.

Uniform

setLayout(new PnutsLayout("cols=3,halign=fill,uniform=x"));
add(new Button("OK"));
add(new Button("Cancel Command"));
add(new Button("Help"));

While widths of bounding-boxes usually differ column by column, they can be as long as the longest one uniformly.

Expansion

A size of surrounding container usually does not affect the size of a bounding-box. When a property expand is specified a bounding-box can be expanded or shrunk as the container.

setLayout(new PnutsLayout("cols=3,halign=left,expand=y"));
add(new Button("OK"));
add(new Button("Cancel"));
add(new Button("Help"));
setLayout(new PnutsLayout("cols=3,expand=y"));
add(new Button("OK"));
add(new Button("Cancel"));
add(new Button("Help", "halign=fill,expand=x"));
setLayout(new PnutsLayout("cols=3,expand=xy"));
add(new Button("OK"));
add(new Button("Cancel"));
add(new Button("Help"));
setLayout(new PnutsLayout("cols=3,halign=fill,valign=fill,expand=xy"));
add(new Button("OK"));
add(new Button("Cancel"));
add(new Button("Help"));

Back