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:
property description default constructor add() cols Number of columns. 1 OK - 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 - colspan number of columns the component occupies 1 - OK rowspan number of rows the component occupies 1 - OK padx external padding in x 0 OK OK pady external padding in y 0 OK OK ipadx internal padding in x. The added component gets larger in x. 0 OK OK ipady internal padding in y. The added component gets larger in y. 0 OK OK halign alignment of x. One of "left", "right","center","fill" center OK OK valign alignment of y. One of "top", "bottom", "center", "fill" center OK OK 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. "" OK OK
![]() |
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.
![]() |
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.
![]() |
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.
![]() |
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")); |
![]() |
setLayout(new PnutsLayout("halign=fill")); add(new Button("OK")); add(new Button("Cancel Command")); add(new Button("Help")); |
![]() |
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.
![]() |
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.
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")); |