com.darwinsys.swingui.layout.ColumnLayout Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of darwinsys-api Show documentation
Show all versions of darwinsys-api Show documentation
Ian Darwin's assorted Java stuff,
assembled as an API.
package com.darwinsys.swingui.layout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager;
/**
*
* ColumnLayout, a Single-Column 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/
*/
public class ColumnLayout implements LayoutManager {
/** Constant for X AXIS (horizontal column) alignment */
public static final int X_AXIS = 'x';
/** Constant for Y AXIS (vertical column) alignment */
public static final int Y_AXIS = 'y';
/** The alignment for this ColumnLayout */
protected final int alignment;
/** The X padding for this ColumnLayout */
protected final int hPadding; // blank final
/** The Y padding for this ColumnLayout */
protected final int vPadding; // blank final
/** The minimum width of each component */
protected int minw;
/** The minimum height of each component */
protected int minh;
/** The list of components */
Component[] curComps;
/** Construct a ColumnLayout given only an alignment.
* @param dirn The direction
*/
public ColumnLayout(int dirn) {
this(dirn, 0, 0);
}
/** Construct a ColumnLayout given an alignment and a padding amount.
* @param dirn The direction
* @param pad The padding, in px
*/
public ColumnLayout(int dirn, int pad) {
this(dirn, pad, pad);
}
/** Construct a ColumnLayout given an alignment and h,v padding amounts.
* @param dirn The direction
* @param hpad The horizontal padding, in px
* @param vpad The vertical padding, in px
*/
public ColumnLayout(int dirn, int hpad, int vpad) {
alignment = dirn;
hPadding = hpad;
vPadding = vpad;
}
/**
* 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.
*/
public void addLayoutComponent(String name, Component c) {
System.err.println("don't use add(component,name) with ColumnLayout");
}
/**
* 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.
*/
public void layoutContainer(Container target) {
//System.out.println("ColumnLayout.layoutContainer() called.");
doLayout(target);
}
/** Used internally: compute the layout and the maximal preferred
* width and height
*
* TODO XXX NEED TO SCALE BY TARGSIZE?
* @param target The target container
* @return The computed size
*/
protected Dimension doLayout(Container target) {
// Pass 1 - get preferred sizes
minw = minh = 0;
curComps = target.getComponents();
for (int i = 0; i