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.
setLayout(new PnutsLayout("columns = 3"));
add(button1, "padding = 20");
add(button2, "colspan = 2");
add(button3, "align = top:left");
Properties are:
Property Meaning Default constructor add() columns The number of columns. 1 true false uniform Sets the width or height to be the same for each column. Can be x/y/xy/none. none true false colspan Number of columns the component occupies. 1 false true rowspan Number of rows the component occupies. 1 false true spacing Minimum spacing that will be put around the component. Can be a single value (5) or top:right:bottom:left (eg 5:10:5:10). 0 true true padding Padding around the component. Can be a single value (5) or top:right:bottom:left (eg 5:10:5:10). 0 true true ipadding Padding inside the component (making it larger). Can be a single value (5) or x:y (eg 5:10). 0 true true fill Sets the width and/or height of the component to a percentage of the cell width. Can be x/y/xy/none or a single value (5) or x:y (eg 25:75). 0 or none sizes the component to its preferred size. none true true align Alignment of the component in the cell. Can be top/bottom/left/right/center or top/bottom/center:left/right/center (eg top:right). center true true expand Expands the size of the cell so the table takes up the size of the container. Can be x/y/xy/none. none true true border Draws the grid borders of the table. Great for debugging. Can be true/false. false true true
|
setLayout(new PnutsLayout("columns=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("columns=3,padding=4: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 laying out 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("columns=3,ipadding=4: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("align=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 align is specified, a component can be left/right and/or top/bottom aligned.
|
setLayout(new PnutsLayout("align=left,padding=5"));
add(new Button("OK"));
add(new Button("Cancel Command"));
add(new Button("Help"));
|
|
setLayout(new PnutsLayout("fill=x"));
add(new Button("OK"));
add(new Button("Cancel Command"));
add(new Button("Help"));
|
|
setLayout(new PnutsLayout("columns=2,fill=x"));
add(new Button("OK"), "rowspan=2,fill=y");
add(new Button("Cancel"));
add(new Button("Help"));
|
A component can occupy multiple columns or rows.
|
setLayout(new PnutsLayout("columns=3,fill=x,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("columns=3,align=left,expand=y"));
add(new Button("OK"));
add(new Button("Cancel"));
add(new Button("Help"));
|
|
setLayout(new PnutsLayout("columns=3,expand=y"));
add(new Button("OK"));
add(new Button("Cancel"));
add(new Button("Help", "fill=x,expand=x"));
|
|
setLayout(new PnutsLayout("columns=3,expand=xy"));
add(new Button("OK"));
add(new Button("Cancel"));
add(new Button("Help"));
|
|
setLayout(new PnutsLayout("columns=3,fill=xy,expand=xy"));
add(new Button("OK"));
add(new Button("Cancel"));
add(new Button("Help"));
|