com.darwinsys.swingui.layout
Class RelativeLayout

java.lang.Object
  extended by com.darwinsys.swingui.layout.RelativeLayout
All Implemented Interfaces:
java.awt.LayoutManager

public class RelativeLayout
extends java.lang.Object
implements java.awt.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/

Field Summary
protected  java.util.List<com.darwinsys.swingui.layout.RelativeLayout.Tracker> curComps
          to track Components added by named add form.
protected  int curHgt
          actual size height when laid out
protected  int curWid
          actual size width when laid out
protected  int reqHgt
          requested absolute height of canvas
protected  int reqWid
          requested absolute width of canvas
 
Constructor Summary
RelativeLayout(int wid, int ht)
          Constructs an RelativeLayout, given original hard-coded size of panel.
 
Method Summary
 void addLayoutComponent(java.awt.Component comp, java.lang.Object constraint)
          This version is here in preparation for implementing LayoutManager2
 void addLayoutComponent(java.lang.String name, java.awt.Component comp)
          Called by AWT when the user uses the form add(name, Component).
 void layoutContainer(java.awt.Container target)
          Called by AWT to lay out the components in the target Container at its current size.
 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.
 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.
 void removeLayoutComponent(java.awt.Component c)
          Called by AWT to remove a given component from the layout.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

reqWid

protected int reqWid
requested absolute width of canvas


reqHgt

protected int reqHgt
requested absolute height of canvas


curWid

protected int curWid
actual size width when laid out


curHgt

protected int curHgt
actual size height when laid out


curComps

protected java.util.List<com.darwinsys.swingui.layout.RelativeLayout.Tracker> curComps
to track Components added by named add form.

Constructor Detail

RelativeLayout

public RelativeLayout(int wid,
                      int ht)
Constructs an RelativeLayout, given original hard-coded size of panel.

Method Detail

addLayoutComponent

public void addLayoutComponent(java.lang.String name,
                               java.awt.Component comp)
Called by AWT when the user uses the form add(name, Component). Adds the specified component with the specified name to the layout.

Specified by:
addLayoutComponent in interface java.awt.LayoutManager
Parameters:
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.
comp - Component to be added.

addLayoutComponent

public void addLayoutComponent(java.awt.Component comp,
                               java.lang.Object constraint)
This version is here in preparation for implementing LayoutManager2


layoutContainer

public void layoutContainer(java.awt.Container target)
Called by AWT to lay out the components in the target Container at its current size.

Specified by:
layoutContainer in interface java.awt.LayoutManager
Parameters:
target - Container whose components are to be laid out.

minimumLayoutSize

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.

Specified by:
minimumLayoutSize in interface java.awt.LayoutManager
Parameters:
target - Container to calculate for

preferredLayoutSize

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.

Specified by:
preferredLayoutSize in interface java.awt.LayoutManager
Parameters:
target - Container to calculate for

removeLayoutComponent

public void removeLayoutComponent(java.awt.Component c)
Called by AWT to remove a given component from the layout.

Specified by:
removeLayoutComponent in interface java.awt.LayoutManager
Parameters:
c - Component to be removed


Copyright © 1996-2004 Ian F. Darwin. See license.html for usage license.