I am using BorderLayout. What I mean is that there is a way to turn on live
resizing so that the contents of the window are resized in real time as the
window's resize borders are being dragged but I can't remember what it is
and looking through the JFrame JavaDoc hasn't helped.
Wes
This is a layout manager I worked with in some project, it's a rework of
some layout manager I found at sun's tutorial. It uses the Java 1.5
implementations so there could be some work around (ie the Hashtable) to
make it 1.4 compatible.
-Yanick
// ***************************************************************
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
public class PointLayout
implements LayoutManager2 {
private Dimension reference; //the reference
bounding box
private Hashtable<Component,Rectangle2D.Double> compTable; //constraints
(Rectangles)
private boolean scaling;
/**
* Default constructor: same as PointLayout( true );
*/
public PointLayout() {
this( true );
}
/**
* Constructor: specify the scaling enabled/disabled
* @param scale boolean true si les composants sont redimensionn?s
automatiquement
*/
public PointLayout( boolean scale ) {
compTable = new Hashtable<Component,Rectangle2D.Double>();
reference = null;
scaling = scale;
}
/**
* Activate or deactivate the auto scaling of components
* @param scale boolean true for enabled, false else
*/
public void setAutoScale( boolean scale ) {
scaling = scale;
}
/**
* Return if the components are scaled when resizing
* @return boolean true if components are scaled, false else
*/
public boolean isAutoScaling() {
return scaling;
}
/**
* Adds the specified component to the layout, using the specified
* constraint object.
* @param comp the component to be added
* @param constraints where/how the component is added to the layout.
*/
public void addLayoutComponent(Component comp, Object constraints) {
if (constraints instanceof Rectangle) {
Rectangle rect = (Rectangle) constraints;
if (rect.width <= 0 || rect.height <= 0) {
throw new IllegalArgumentException(
"cannot add to layout: rectangle must have positive width and
height");
}
// ******* optional : prevent negative (not visible) components
**********
//if (rect.x + rect.width < 0 || rect.y + rect.height < 0) {
// throw new IllegalArgumentException(
// "cannot add to layout: rectangle x and y must be >= 0");
//}
setConstraints(comp, rect);
}
else if (constraints != null) {
throw new IllegalArgumentException(
"cannot add to layout: constraint must be a Rectangle");
}
/**
else {
throw new IllegalArgumentException(
"cannot add to layout: you must specify a constraint");
}
*/
}
public void setConstraints(Component comp, Rectangle constraints) {
compTable.put(comp, new Rectangle2D.Double(
(double) constraints.x,
(double) constraints.y,
(double) constraints.width,
(double) constraints.height));
}
/**
* Adds the specified component with the specified name to
* the layout. This does nothing, since constraints are required.
*/
public void addLayoutComponent(String name, Component comp) {
throw new Exception( "Method not allowed; constraints required" );
}
/**
* Removes the specified component from the layout.
* @param comp the component to be removed
*/
public void removeLayoutComponent(Component comp) {
compTable.remove(comp);
}
/**
* Lays out the container in the specified container.
* @param parent the component which needs to be laid out
*/
public void layoutContainer(Container parent) {
synchronized (parent.getTreeLock()) {
if (parent.getSize().getWidth() > 0 && parent.getSize().getHeight() >
0) {
if ( scaling ) { // if scaling is ON
Insets insets = parent.getInsets();
int ncomponents = parent.getComponentCount();
if (ncomponents == 0) {
return;
}
double xfactor = 1.0d;
double yfactor = 1.0d;
if (reference != null) {
xfactor = parent.getWidth() / reference.getWidth();
yfactor = parent.getHeight() / reference.getHeight();
}
Rectangle2D.Double rect;
Component c;
for (int i = 0; i < ncomponents; i++) {
c = parent.getComponent(i);
rect = compTable.get(c);
if (rect != null) {
rect.setRect( (rect.getX() * xfactor),
(rect.getY() * yfactor),
(rect.getWidth() * xfactor),
(rect.getHeight() * yfactor));
c.setBounds(insets.left + (int) rect.getX(),
insets.top + (int) rect.getY(),
(int) rect.getWidth(), (int) rect.getHeight());
}
}
}
reference = parent.getSize(); // get the parent size
}
}
}
/**
* Calculates the minimum size dimensions for the specified
* panel given the components in the specified parent container.
* @param parent the component to be laid out
* @see #preferredLayoutSize
*/
public Dimension minimumLayoutSize(Container parent) {
return getLayoutSize(parent);
}
public Dimension preferredLayoutSize(Container parent) {
return getLayoutSize(parent);
}
/**
* Returns the maximum size of this component.
* @see java.awt.Component#getMinimumSize()
* @see java.awt.Component#getPreferredSize()
* @see LayoutManager
*/
public Dimension maximumLayoutSize(Container target) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
return new Dimension(screenSize.width, screenSize.height);
}
/**
* Algorithm for calculating layout size (minimum or actual).
*
* @param parent the container in which to do the layout.
* @return the dimensions to lay out the subcomponents of the specified
* container.
*/
protected Dimension getLayoutSize(Container parent) {
int ncomponents = parent.getComponentCount();
Dimension minSize = parent.getSize();
for (int i = 0; i < ncomponents; i++) {
Component c = parent.getComponent(i);
Rectangle2D.Double rect = (Rectangle2D.Double) compTable.get(c);
if ((c != null) && (rect != null)) {
minSize.width = Math.max(minSize.width, (int) (rect.getX() +
rect.getWidth()));
minSize.height = Math.max(minSize.height, (int) (rect.y +
rect.getHeight()));
}
}
return minSize;
}
/**
* Returns the alignment along the x axis. This specifies how
* the component would like to be aligned relative to other
* components. The value should be a number between 0 and 1
* where 0 represents alignment along the origin, 1 is aligned
* the furthest away from the origin, 0.5 is centered, etc.
*/
public float getLayoutAlignmentX(Container target) {
return 0f;
}
/**
* Returns the alignment along the y axis. This specifies how
* the component would like to be aligned relative to other
* components. The value should be a number between 0 and 1
* where 0 represents alignment along the origin, 1 is aligned
* the furthest away from the origin, 0.5 is centered, etc.
*/
public float getLayoutAlignmentY(Container target) {
return 0f;
}
/**
* Invalidates the layout, indicating that if the layout manager
* has cached information it should be discarded.
*/
public void invalidateLayout(Container target) {
// Do nothing
}
}