Home Java Java-tips Summaries Summary - GUI Layouts 2 - BoxLayout, CardLayout, GridbagLayout

Ask Questions?

View Latest Questions

Advertisement


 
 

Summary - GUI Layouts 2 - BoxLayout, CardLayout, GridbagLayout
Posted on: July 27, 2006 at 12:00 AM
Layout, layo, becomes that layout manager for container p (a JPanel or Container) with the following.

Java: Summary - GUI Layouts 2 - BoxLayout, CardLayout, GridbagLayout

In this section we will learn important methods of GUI Layouts.

Following Layouts are discussed:

a) BoxLayout

b) CardLayout

c) GridBagLayout

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

   p.setLayout(layo);

Layouts (BoxLayout, CardLayout, GridbagLayout)

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
Advertisement


DMCA.com