# Java: Summary - GUI Layouts

Layout, layo, becomes that layout manager for container p (essentially JPanel or Container) with the following.

       p.setLayout(layo);

### Layouts (FlowLayout, BorderLayout, GridLayout, BoxLayout, CardLayout)

FlowLayout - Arranges widgets left-to-right, top-to-bottom.
`flow =` | new FlowLayout(); | Creates FlowLayout, centered with 5 pixel gaps.
`flow =` | new FlowLayout(align); | Specifies alignment (`FlowLayout.LEFT`, `FlowLayout.CENTER`, `FlowLayout.RIGHT`).
`flow =` | new FlowLayout(align, h, v); | Specifies alignment and horizontal (h) and vertical (v) gaps in pixels.
| p.add(widget); | Adds widget to the next left-to-right, top-to-bottom position.
BorderLayout - Lays out components in BorderLayout.NORTH, EAST, SOUTH, WEST, and CENTER sections.
`bord =` | new BorderLayout(); | Creates BorderLayout. Widgets added with constraint to tell where.
`bord =` | new BorderLayout(h, v); | Creates BorderLayout with horizonal and vertical gaps sizes in pixels.
| p.add(widget, pos); | Adds widget to one of the 5 border layout regions, pos (see list above).
GridLayout - Lays out components in equal sized rectangular grid, added r-t-l, top-to-bottom.
grid = | new GridLayout(r, c); | Creates GridLayout with specified rows and columns.
`grid =` | new GridLayout(r,c,h,v); | As above but also specifies horizontal and vertical space between cells.
| p.add(widget); | Adds widget to the next left-to-right, top-to-bottom cell.
BoxLayout, Box - Lays out components in single row or col. Add "glue" and rigid areas to control spacing.
boxl = | new BoxLayout(p, dir); | Creates layout for container p. dir is `BoxLayout.X_AXIS` `BoxLayout.Y_AXIS`. Note: must have already created p.
box = | new Box(dir); | Creates a Container with vertical or horizontal layout.
| p.add(widget); | Adds widget to the next position.
| These static Box methods create useful spacing components, which can be used in other layouts.
| Box.createVerticalStrut(n) | Creates a vertical spacer n pixels high.
| Box.createHorizontalStrut(n) | Creates a horizontal spacer n pixels wide.
| Box.createRigidArea(dim) | Spacer with width and height. dim is Dimension object, eg, `new Dimension(10, 0)`.
| Box.createHorizontalGlue() | Creates expandable horizontal space.
| Box.createVerticalGlue() | Creates expandable vertical space.
| new Box.Filler(mn,prf,mx) | Creates a Box.Filler with min, preferred, and max sizes, each is Dimension object.
CardLayout - Panels are placed on top of each other like stack of cards -- only one visible at a time.
| Used to produce installer or wizard style interface as steps in process, or to select palette. Use JTabbedPanel if user should have control.
crdMgr = | new CardLayout(); | Sets layout to CardLayout.
crdMgr = | new CardLayout(h, v)); | Also can specify horizontal and vertical padding.
| p.add(cardPanel); | Adds next card to panel.
| p.add(cardPanel, name); | Adds next card to panel. Use String name to select it later.
| crdMgr.first(p); | Panel p displays the first card in the layout of p.
| crdMgr.next(p); | Panel p displays the next card in the layout of p.
| crdMgr.previous(p); | Panel p displays the previous card in the layout of p.
| crdMgr.last(p); | Panel p displays the last card in the layout of p.
| crdMgr.show(p, name); | Panel p displays card with String name name in panel p.
GridBagLayout - Panel divided into rows&cols of possibly unequal size. Overall best, but difficult.
`gbag =` | new GridBagLayout(); | Creates GridBagLayout.
| Must add with GridBagConstraints object (gbc below). Can (carefully) reuse same gbc object.
gbc = | new GridBagConstraints(); | Creates a new GridBagConsraints object.
| gbc.gridx = i; | Column, counting from 0. Always start at 0. Don't use default (RELATIVE).
| gbc.gridy = i; | Column, counting from 0. Always start at 0. Don't use default (RELATIVE)..
| gbc.gridwidth = i; | Number of columns wide (default 1).
| gbc.gridheight = i; | Number of rows high (default 1).
| gbc.weightx = d; | Relative horizontal space to allocate if container expands. Default 0.0.
| gbc.weighty = d; | Relative vertical space to allocate if container expands. Default 0.0.
| gbc.fill = fill; | How to expand comp if space larger than preferred size. fill can be `GridBagConstraints.NONE, GridBagConstraints.VERTICAL, GridBagConstraints.HORIZONTAL, GridBagConstraints.BOTH`.
| gbc.anchor = anch; | If component doesn't fill the space, tells how it should be aligned. anch can be one of these `GridBagConstraints` constants: `CENTER (default), NORTH, SOUTH, NORTHEAST, SOUTHWEST, EAST, WEST, SOUTHEAST, NORTHWEST`
| gbc.insets = ins; | Space around component. Eg, `gbc.insets = new Insets(10,5,5,10);`
| gbc.ipadx = i; | Horizontal padding internal to component. Can be negative!
| gbc.ipady = i; | Vertical padding internal to component. Can be negative!

Copyleft 2005 Fred Swartz