Class sunsoft.jws.visual.rt.awt.GBLayout
All Packages Class Hierarchy This Package Previous Next Index
Class sunsoft.jws.visual.rt.awt.GBLayout
java.lang.Object
|
+----sunsoft.jws.visual.rt.awt.GBLayout
- public class GBLayout
- extends Object
- implements LayoutManager
GBLayout is a flexible layout manager
that aligns components vertically and horizontally,
without requiring that the components be the same size.
Each GBLayout uses a rectangular grid of cells,
with each component occupying one or more cells
(called its display area).
Each component managed by a GBLayout
is associated with a
GBConstraints instance
that specifies how the component is laid out
within its display area.
How a GBLayout places a set of components
depends on each component's GBConstraints and minimum size,
as well as the preferred size of the components' container.
To use a GBLayout effectively,
you must customize one or more of its components' GBConstraints.
You customize a GBConstraints object by setting one or more
of its instance variables:
- gridx,
gridy
- Specifies the cell at the upper left of the component's display area,
where the upper-left-most cell has address gridx=0, gridy=0.
Use GBConstraints.RELATIVE (the default value)
to specify that the component be just placed
just to the right of (for gridx)
or just below (for gridy)
the component that was added to the container
just before this component was added.
- gridwidth,
gridheight
- Specifies the number of cells in a row (for gridwidth)
or column (for gridheight)
in the component's display area.
The default value is 1.
Use GBConstraints.REMAINDER to specify
that the component be the last one in its row (for gridwidth)
or column (for gridheight).
Use GBConstraints.RELATIVE to specify
that the component be the next to last one
in its row (for gridwidth) or column (for gridheight).
- fill
- Used when the component's display area
is larger than the component's requested size
to determine whether (and how) to resize the component.
Valid values are
GBConstraints.NONE
(the default),
GBConstraints.HORIZONTAL
(make the component wide enough to fill its display area
horizontally, but don't change its height),
GBConstraints.VERTICAL
(make the component tall enough to fill its display area
vertically, but don't change its width),
and
GBConstraints.BOTH
(make the component fill its display area entirely).
- ipadx,
ipady
- Specifies the internal padding:
how much to add to the minimum size of the component.
The width of the component will be at least
its minimum width plus ipadx*2 pixels
(since the padding applies to both sides of the component).
Similarly, the height of the component will be at least
the minimum height plus ipady*2 pixels.
- insets
- Specifies the external padding of the component --
the minimum amount of space between the component
and the edges of its display area.
- anchor
- Used when the component is smaller than its display area
to determine where (within the area) to place the component.
Valid values are
GBConstraints.CENTER (the default),
GBConstraints.NORTH,
GBConstraints.NORTHEAST,
GBConstraints.EAST,
GBConstraints.SOUTHEAST,
GBConstraints.SOUTH,
GBConstraints.SOUTHWEST,
GBConstraints.WEST, and
GBConstraints.NORTHWEST.
- weightx,
weighty
- Used to determine how to distribute space;
this is important for specifying resizing behavior.
Unless you specify a weight
for at least one component in a row (weightx)
and column (weighty),
all the components clump together in the center of
their container.
This is because when the weight is zero (the default),
the GBLayout puts any extra space
between its grid of cells and the edges of the container.
The following figure shows ten components (all buttons)
managed by a GBLayout:
All the components have fill=GBConstraints.BOTH.
In addition, the components have the following non-default constraints:
- Button1, Button2, Button3:
weightx=1.0
- Button4:
weightx=1.0,
gridwidth=GBConstraints.REMAINDER
- Button5:
gridwidth=GBConstraints.REMAINDER
- Button6:
gridwidth=GBConstraints.RELATIVE
- Button7:
gridwidth=GBConstraints.REMAINDER
- Button8:
gridheight=2, weighty=1.0,
- Button9, Button 10:
gridwidth=GBConstraints.REMAINDER
Here is the code that implements the example shown above:
import java.awt.*;
import java.util.*;
import java.applet.Applet;
public class GridBagEx1 extends Applet {
protected void makebutton(String name,
GBLayout gridbag,
GBConstraints c) {
Button button = new Button(name);
gridbag.setConstraints(button, c);
add(button);
}
public void init() {
GBLayout gridbag = new GBLayout();
GBConstraints c = new GBConstraints();
setFont(new Font("Helvetica", Font.PLAIN, 14));
setLayout(gridbag);
c.fill = GBConstraints.BOTH;
c.weightx = 1.0;
makebutton("Button1", gridbag, c);
makebutton("Button2", gridbag, c);
makebutton("Button3", gridbag, c);
c.gridwidth = GBConstraints.REMAINDER; //end row
makebutton("Button4", gridbag, c);
c.weightx = 0.0; //reset to the default
makebutton("Button5", gridbag, c); //another row
c.gridwidth = GBConstraints.RELATIVE; //next-to-last in row
makebutton("Button6", gridbag, c);
c.gridwidth = GBConstraints.REMAINDER; //end row
makebutton("Button7", gridbag, c);
c.gridwidth = 1; //reset to the default
c.gridheight = 2;
c.weighty = 1.0;
makebutton("Button8", gridbag, c);
c.weighty = 0.0; //reset to the default
c.gridwidth = GBConstraints.REMAINDER; //end row
c.gridheight = 1; //reset to the default
makebutton("Button9", gridbag, c);
makebutton("Button10", gridbag, c);
resize(300, 100);
}
public static void main(String args[]) {
Frame f = new Frame("GridBag Layout Example");
GridBagEx1 ex1 = new GridBagEx1();
ex1.init();
f.add("Center", ex1);
f.pack();
f.resize(f.preferredSize());
f.show();
}
}
-
anchor
-
-
clipAnchor
-
-
columnWeights
- Determines the minimum weights for the grid columns.
-
columnWidths
- Determines the minimum widths for the grid columns.
-
comptable
-
-
defaultConstraints
-
-
INITGRIDSIZE
-
-
layoutInfo
-
-
MINSIZE
-
-
PREFERREDSIZE
-
-
rowHeights
- Determines the minimum heights for the grid rows.
-
rowWeights
- Determines the minimum weights for the grid rows.
-
TINYSIZE
-
-
GBLayout()
- Creates a gridbag layout.
-
addLayoutComponent(String, Component)
- Adds the specified component with the specified name to the layout.
-
AdjustForGravity(GBConstraints, Rectangle)
- Adjusts the x, y, width, and height fields to the correct
values according to the constraint geometry and pads.
-
ArrangeGrid(Container)
- Lays out the grid.
-
ArrangeGrid(Container, boolean)
- Lays out the grid, conditionally reshaping the children.
-
disable()
- Globally disable gridbag layout.
-
enable()
- Globally enable gridbag layout.
-
getAnchor()
- Returns the current anchor.
-
getClipAnchor()
- Returns the current clip anchor.
-
getConstraints(Component)
- Retrieves the constraints for the specified component.
-
getLayoutDimensions()
- Returns the widths and heights of the grid columns and rows.
-
GetLayoutInfo(Container, int)
- Fill in an instance of the GBLayoutInfo structure for the
current set of managed children.
-
getLayoutOrigin()
- Returns the coordinates of the upper-left corner of the grid.
-
getLayoutWeights()
- Returns the current set of weights for the grid columns and rows.
-
getMinimumLayoutDimensions(Container)
- Returns the minimum widths and heights of the grid columns and rows.
-
GetMinSize(Container, GBLayoutInfo)
- Figure out the minimum size of the
parent based on the information retrieved from GetLayoutInfo.
-
getPreferredLayoutDimensions(Container)
- Returns the preferred widths and heights of the grid columns and rows.
-
layoutContainer(Container)
-
Lays out the container in the specified panel.
-
layoutContainerNoReshape(Container)
- Does everything that layout normally does, except the components
aren't actually reshaped.
-
location(int, int)
- Returns the coordinates of the grid cell corresponding to the given
pixel coordinates.
-
lookupConstraints(Component)
- Retrieves the constraints for the specified component.
-
minimumLayoutSize(Container)
- Returns the minimum dimensions needed to layout the components
contained in the specified panel.
-
preferredLayoutSize(Container)
-
Returns the preferred dimensions for this layout given the components
in the specified panel.
-
removeLayoutComponent(Component)
- Removes the specified component from the layout.
-
setAnchor(int)
- Sets the anchor for the gridbag.
-
setClipAnchor(int)
- Sets the clip anchor.
-
setConstraints(Component, GBConstraints)
- Sets the constraints for the specified component.
-
setConstraints(Component, String)
- Sets the constraints from an option string.
-
tinyLayoutSize(Container)
- Returns the smallest allowable size for the specified panel.
-
toString()
- Returns the String representation of this GBLayout's values.
columnWidths
public int columnWidths[]
- Determines the minimum widths for the grid columns.
rowHeights
public int rowHeights[]
- Determines the minimum heights for the grid rows.
columnWeights
public double columnWeights[]
- Determines the minimum weights for the grid columns.
rowWeights
public double rowWeights[]
- Determines the minimum weights for the grid rows.
INITGRIDSIZE
protected final static int INITGRIDSIZE
MINSIZE
protected final static int MINSIZE
PREFERREDSIZE
protected final static int PREFERREDSIZE
TINYSIZE
protected final static int TINYSIZE
comptable
protected Hashtable comptable
defaultConstraints
protected GBConstraints defaultConstraints
layoutInfo
protected GBLayoutInfo layoutInfo
anchor
protected int anchor
clipAnchor
protected int clipAnchor
GBLayout
public GBLayout()
- Creates a gridbag layout.
enable
public static synchronized void enable()
- Globally enable gridbag layout. The default is for gridbag layout to
be enabled.
disable
public static synchronized void disable()
- Globally disable gridbag layout. This can be used to improve
performance by temporarily disabling layout during
spurious calls to validate.
setConstraints
public void setConstraints(Component comp,
GBConstraints constraints)
- Sets the constraints for the specified component.
- Parameters:
- comp - the component to be modified
- constraints - the constraints to be applied
setConstraints
public void setConstraints(Component comp,
String constraints)
- Sets the constraints from an option string.
Each option has the form key=value. Options are separated by
semicolons (;).
- Parameters:
- comp - the component to be modified
- constraints - the constraints string
getConstraints
public GBConstraints getConstraints(Component comp)
- Retrieves the constraints for the specified component. A copy of
the constraints is returned.
- Parameters:
- comp - the component to be queried
lookupConstraints
protected GBConstraints lookupConstraints(Component comp)
- Retrieves the constraints for the specified component. The return
value is not a copy, but is the actual constraints class used by the
layout mechanism.
- Parameters:
- comp - the component to be queried
getLayoutOrigin
public Point getLayoutOrigin()
- Returns the coordinates of the upper-left corner of the grid.
The coordinates are based on the current layout of the grid.
getLayoutDimensions
public int[][] getLayoutDimensions()
- Returns the widths and heights of the grid columns and rows.
The dimensions are based on the current layout of the grid.
getMinimumLayoutDimensions
public int[][] getMinimumLayoutDimensions(Container parent)
- Returns the minimum widths and heights of the grid columns and rows.
This is how the grid would be arranged if the parent were
to be reshaped to its minimum size.
getPreferredLayoutDimensions
public int[][] getPreferredLayoutDimensions(Container parent)
- Returns the preferred widths and heights of the grid columns and rows.
This is how the grid would be arranged if the parent were
to be reshaped to its preferred size.
getLayoutWeights
public double[][] getLayoutWeights()
- Returns the current set of weights for the grid columns and rows.
The return value reflects the effective weights for the columns
and rows after taking into account the weight constraints that
are set on the child components.
location
public Point location(int x,
int y)
- Returns the coordinates of the grid cell corresponding to the given
pixel coordinates.
setAnchor
public void setAnchor(int anchor)
- Sets the anchor for the gridbag. The anchor determines the placement
for the child components when the container has extra space and none
of the children have weights. The default anchor is CENTER.
getAnchor
public int getAnchor()
- Returns the current anchor.
setClipAnchor
public void setClipAnchor(int clipAnchor)
- Sets the clip anchor. The clip anchor determines which edge(s) of
the container get clipped when there is not enough space. The
default clip anchor is NORTHWEST. A clip anchor of NORTHWEST means
that northwest corner is anchored, so the south and east edges will
be clipped if there is not enough space.
getClipAnchor
public int getClipAnchor()
- Returns the current clip anchor.
addLayoutComponent
public void addLayoutComponent(String name,
Component comp)
- Adds the specified component with the specified name to the layout.
The name is converted to a set of GBConstraints.
- Parameters:
- name - the constraints string
- comp - the component to be added
removeLayoutComponent
public void removeLayoutComponent(Component comp)
- Removes the specified component from the layout. Does not apply.
- Parameters:
- comp - the component to be removed
preferredLayoutSize
public Dimension preferredLayoutSize(Container parent)
- Returns the preferred dimensions for this layout given the components
in the specified panel.
- Parameters:
- parent - the component which needs to be laid out
- See Also:
- minimumLayoutSize
minimumLayoutSize
public Dimension minimumLayoutSize(Container parent)
- Returns the minimum dimensions needed to layout the components
contained in the specified panel.
- Parameters:
- parent - the component which needs to be laid out
- See Also:
- preferredLayoutSize
tinyLayoutSize
public Dimension tinyLayoutSize(Container parent)
- Returns the smallest allowable size for the specified panel.
This can be smaller than minimumSize if there are insets and
pads set on any of the panel's children.
- Parameters:
- parent - the component which needs to be laid out
- See Also:
- preferredLayoutSize
layoutContainer
public void layoutContainer(Container parent)
- Lays out the container in the specified panel.
- Parameters:
- parent - the specified component being laid out
- See Also:
- Container
layoutContainerNoReshape
public void layoutContainerNoReshape(Container parent)
- Does everything that layout normally does, except the components
aren't actually reshaped. This has the useful side effect of
setting the location and size variables in the constraints
for each component.
- Parameters:
- parent - the specified component being laid out
- See Also:
- Container
toString
public String toString()
- Returns the String representation of this GBLayout's values.
- Overrides:
- toString in class Object
GetLayoutInfo
protected GBLayoutInfo GetLayoutInfo(Container parent,
int sizeflag)
- Fill in an instance of the GBLayoutInfo structure for the
current set of managed children. This requires four passes
through the child components:
1) Figure out the dimensions of the layout grid.
2) Determine which cells the components occupy.
3) Distribute the weights among the rows/columns.
4) Distribute the minimum sizes among the rows/columns.
This also caches the minsizes for all the children when they are
first encountered (so subsequent loops don't need to ask again).
AdjustForGravity
protected void AdjustForGravity(GBConstraints c,
Rectangle r)
- Adjusts the x, y, width, and height fields to the correct
values according to the constraint geometry and pads.
GetMinSize
protected Dimension GetMinSize(Container parent,
GBLayoutInfo info)
- Figure out the minimum size of the
parent based on the information retrieved from GetLayoutInfo.
ArrangeGrid
protected void ArrangeGrid(Container parent)
- Lays out the grid. Called directly from layoutContainer.
ArrangeGrid
protected void ArrangeGrid(Container parent,
boolean doReshape)
- Lays out the grid, conditionally reshaping the children.
The doReshape flag indicates whether or not
the children should be reshaped.
- See Also:
- layoutContainerNoReshape
All Packages Class Hierarchy This Package Previous Next Index