RelativeLayoutpublic 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! |
Fields Summary |
---|
protected int | reqWidrequested absolute width of canvas | protected int | reqHgtrequested absolute height of canvas | protected int | curWidactual size width when laid out | protected int | curHgtactual size height when laid out | protected Vector | curCompsto 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 void | addLayoutComponent(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.
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 void | layoutContainer(java.awt.Container target)Called by AWT to lay out the components
in the target Container at its current size.
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.Dimension | minimumLayoutSize(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.
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.Dimension | preferredLayoutSize(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.
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 void | removeLayoutComponent(java.awt.Component c)Called by AWT to remove a given component from the layout.
curComps.removeElement(c);
|
|