FileDocCategorySizeDatePackage
RelativeLayout.javaAPI DocExample6013Mon May 31 22:51:38 BST 2004com.darwinsys.swingui.layout

RelativeLayout

public class RelativeLayout extends Object implements LayoutManager

RelativeLayout, a Relative Layout Manager for Java J2SE. Mainly for porting tired old code that uses x,y locations. You really can't just assign x,y locations to components in Java Java J2SE - it breaks badly when the user resizes (and you can not mandate that the user can't resize you -- see any book on UI design for that little discussion -- and can also look bad due to resolution independance. Symantec Cafe 1.x, for example, used to spit out unfortunate (and unmaintainable) code like this:

setLayout(null);
setSize(331,241);
label1=new Label("Info Applet", Label.CENTER);
add(label1);
label1.setBounds(91,19,107,15);

Bleaarrgghh!!! To make it work properly at all resolutions and survive user-initiated resize actions, change it to

setLayout(new RelativeLayout(331,241,false);
label1=new Label("Info Applet", Label.CENTER);
add("91,19", label1);
Note that it's actually less work to get it right. Symantec, Microsoft, and others, please take note!

author
Ian Darwin, http://www.darwinsys.com/

Fields Summary
protected int
reqWid
requested absolute width of canvas
protected int
reqHgt
requested absolute height of canvas
protected int
curWid
actual size width when laid out
protected int
curHgt
actual size height when laid out
protected Vector
curComps
to track Components added by named add form.
Constructors Summary
public RelativeLayout(int wid, int ht)
Constructs an RelativeLayout, given original hard-coded size of panel.


	         	 
	     
		this.reqWid = wid;
		this.reqHgt = ht;
	
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.

param
name String with location for component c Note: the "name" must contain x, y location, ie.,
add("" + 320 + "," + 100, new Button("Quit"));
or
add("320,100", new Button("Quit").
This adds the Button at x=320, y=100 when the Panel is at its original full size.
param
c Component to be added.

		int x, y;
		StringTokenizer st = new StringTokenizer(name, ",");
		x = Integer.parseInt(st.nextToken());
		y = Integer.parseInt(st.nextToken());
		// System.out.println("Adding: Name " + name +"; obj " + c
		//	 + "; x " + x + "; y " + y);
		Tracker t = new Tracker(x, y, c);
		curComps.addElement(t);
	
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.

		Dimension targSize = target.getSize();
		Insets ins = target.getInsets();
		// System.out.println("layoutContainer: size " + targSize);
		curWid = targSize.width;
		curHgt = targSize.height;
		float widRatio = (float)curWid / (float)reqWid;
		float hgtRatio = (float)curHgt / (float)reqHgt;
		for (int i = 0; i<curComps.size(); i++) {
			int px, py, pw, ph;
			Tracker t = (Tracker)curComps.elementAt(i);
			Component tc = t.getComponent();
			Dimension d = tc.getPreferredSize();
			px = ins.right+(int)(t.getRequestedLoc().x * widRatio);
			py = ins.top + (int)(t.getRequestedLoc().y * hgtRatio);
			pw = d.width;
			ph = d.height;
			// System.out.println("layoutContainer["+i+"]: move " +
			// tc + " to " + px + ", " + py);
			tc.setBounds(px, py, pw, ph);
		}
	
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

		int minw = 0, minh = 0;
		for (int i = 0; i<curComps.size(); i++) {
			Tracker t = (Tracker)curComps.elementAt(i);
			Component tc = t.getComponent();
			Dimension d = tc.getMinimumSize();
			Point rl = t.getRequestedLoc();
			minw = Math.max(minw, rl.x +d.width);
			minh = Math.max(minh, rl.y +d.height);
			// System.out.println("minLay, minw = " + minw 
			// + "; minh = " + minh);
		}
		return new Dimension(minw, minw);
	
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

		int prefw = 0, prefh = 0;
		for (int i = 0; i<curComps.size(); i++) {
			Tracker t = (Tracker)curComps.elementAt(i);
			Component tc = t.getComponent();
			Dimension d = tc.getMinimumSize();
			Point rl = t.getRequestedLoc();
			prefw = Math.max(prefw, rl.x+d.width);
			prefh = Math.max(prefh, rl.y+d.height);
			// System.out.println("prefLay, prefw = " + 
			// prefw + "; prefh = " + prefh);
		}
		return new Dimension(prefw, prefh);
	
public voidremoveLayoutComponent(java.awt.Component c)
Called by AWT to remove a given component from the layout.

param
c Component to be removed

		curComps.removeElement(c);