FileDocCategorySizeDatePackage
ColumnLayout.javaAPI DocExample5409Sun Mar 14 14:36:44 GMT 2004com.darwinsys.swingui.layout

ColumnLayout

public class ColumnLayout extends Object implements LayoutManager

ColumnLayout, a Columnar Layout Manager for AWT/Swing. Sort of a cross between BoxLayout and GridLayout(0, x). Displays components in a single row or column based on the alignment given to the constructor, with optional padding.

There is a utility method for adding space between the previous and next components, which (like Menu.addSeparator()) adds a fixed-size non-visible component between two added components.

Note: The current version of ColumnLayout doesn't resize.

author
Ian Darwin, http://www.darwinsys.com/
version
$Id: ColumnLayout.java,v 1.10 2004/03/14 20:36:44 ian Exp $

Fields Summary
public static final int
X_AXIS
Constant for X AXIS (horizontal column) alignment
public static final int
Y_AXIS
Constant for Y AXIS (vertical column) alignment
protected final int
alignment
The alignment for this ColumnLayout
protected final int
hPadding
The X padding for this ColumnLayout
protected final int
vPadding
The Y padding for this ColumnLayout
protected int
minw
The minimum width of each component
protected int
minh
The minimum height of each component
Component[]
curComps
The list of components
Constructors Summary
public ColumnLayout(int dirn)
Construct a ColumnLayout given only an alignment.


	        
	   
		this(dirn, 0, 0);
	
public ColumnLayout(int dirn, int pad)
Construct a ColumnLayout given an alignment and a padding amount.

		this(dirn, pad, pad);
	
public ColumnLayout(int dirn, int hpad, int vpad)
Construct a ColumnLayout given an alignment and h,v padding amounts.

		alignment = dirn;
		hPadding  = hpad;
		vPadding  = vpad;
	
Methods Summary
public voidaddLayoutComponent(java.lang.String name, java.awt.Component c)
Called by AWT when the user uses the form add(name, Component). Adds the specified component with the specified name to the layout. Not necessary to use this form.

param
name String with location for component c
param
c Component to be added.

		System.err.println("don't use add(component,name) with ColumnLayout");
	
public voidaddSpacer(java.awt.Container target)
Utility method to add a "spacer"

		target.add(new Spacer());
	
protected java.awt.DimensiondoLayout(java.awt.Container target)
Used internally: compute the layout and the maximal preferred width & height
TODO XXX NEED TO SCALE BY TARGSIZE?

		Dimension targSize = target.getSize();
		Insets ins = target.getInsets();

		// Pass 1 - get preferred sizes 
		minw = minh = 0;
		curComps = target.getComponents();
		for (int i = 0; i<curComps.length; i++) {
			Component tc = curComps[i];
			Dimension d = tc.getPreferredSize();
			minw = Math.max(minw, d.width);
			minh = Math.max(minh, d.height);
		}


		int x = hPadding, y = vPadding;
		// Pass 2 - move & resize components
		for (int i = 0; i<curComps.length; i++) {
			Component tc = curComps[i];
			Dimension d = tc.getPreferredSize();
			int cx, cy, cw, ch;
			switch (alignment) {
			case X_AXIS:
				if (tc instanceof Spacer)
					cw = d.width;
				else
					cw = minw;
				ch = minh; // d.height;
				cx = x;
				x += cw+hPadding;
				cy = vPadding;
				y  = ch;
				break;
			case Y_AXIS:
				cw = minw; // d.width;
				if (tc instanceof Spacer)
					ch = d.height;
				else
					ch = minh;
				cx = hPadding;
				x = cw;
				cy = y;
				y += ch+vPadding;
				break;
			default:
				throw new IllegalArgumentException("Invalid alignment");
			}
			tc.setBounds(cx, cy, cw, ch);
		}
		switch (alignment) {
			case X_AXIS:
				return new Dimension(x, y+2*vPadding);
			case Y_AXIS:
				return new Dimension(x+2*hPadding, y);
			default:
				throw new IllegalArgumentException("Invalid alignment");
		}
	
public voidlayoutContainer(java.awt.Container target)
Called by AWT to lay out the components in the target Container at its current size.

param
target Container whose components are to be laid out.

		//System.out.println("ColumnLayout.layoutContainer() called.");
		doLayout(target);
	
public java.awt.DimensionminimumLayoutSize(java.awt.Container target)
Called from AWT to calculate the minimum size dimensions for the target panel given the components in it. But we use our own list of named insertions, not the list of Components that the container keeps.

param
target Container to calculate for

		// XXX should return doLayout(target, 0, 0); - add vpad, hpad args
		return doLayout(target);
	
public java.awt.DimensionpreferredLayoutSize(java.awt.Container target)
Called by AWT to compute the preferred size for the target panel given our list of the components that it contains.

param
target Container to calculate for

		System.out.println("preferredLayoutSize() called");
		return doLayout(target);
	
public voidremoveLayoutComponent(java.awt.Component c)
Called by AWT to remove a given component from the layout.

param
c Component to be removed

		// nothing to do! user will (hopefully) invalidate() the target.