relativelayout
Class Binding

java.lang.Object
  extended by relativelayout.Binding
All Implemented Interfaces:
Cloneable

public class Binding
extends Object
implements Cloneable

A Binding represents a dependency of some component's position on the position either of another component or of the surrounding container. To lay out a component at a particular place, construct one or more Bindings to tie that component's position to that of other objects, and then add all the Bindings to a RelativeConstraints object, with which you can then add the component to its parent. For example, to place a JButton 12 pixels below and to the right of the upper right corner of its containing JFrame:

         JFrame frame = new JFrame();
         frame.setLayout(new RelativeLayout());
         
         JButton button = new JButton("Hello");
         
         // Create two Bindings, one for the button's left edge
         // and the other for its top edge:
         Binding buttonLeft = new Binding(Edge.LEFT,
                                          12,
                                          Direction.RIGHT,
                                          Edge.LEFT,
                                          frame);
         Binding buttonTop = new Binding( Edge.TOP,
                                          12,
                                          Direction.BELOW,
                                          Edge.TOP,
                                          frame);
                      
         // Now, create a RelativeConstraints object and add the bindings to it:
         RelativeConstraints buttonConstraints = new RelativeConstraints();
         buttonConstraints.addBindings(buttonLeft, buttonTop);
         
         // Lastly, add the button to the frame with the constraints:
         frame.add(button, buttonConstraints);
         
         frame.setVisible(true);
 
You can think of each Binding like an English sentence: the buttonLeft binding in the example above would be read, "my left edge is 12 pixels to the right of the left edge of frame".

It is important to keep in mind that you can define Bindings between components and other components as well as components and the enclosing frame. This allows you to easily do things like placing text fields to the right of labels:
 Binding rightOfLabel = new Binding(Edge.LEFT, 8, Direction.RIGHT, Edge.RIGHT, label);
 RelativeConstraints fieldConstraints = new RelativeConstraints();
 fieldConstraints.addBinding(rightOfLabel);
 frame.add(field, fieldConstraints);
 
Bindings can also be used to make components dynamically resize themselves. To make a component resize horizontally, simply make a Binding for the component's left edge and another for the component's right edge. For example:
         JFrame frame = new JFrame();
         frame.setLayout(new RelativeLayout());
         
         JTextArea area = new JTextArea("Hello, world!");
         
         // Create three Bindings, one for the area's left edge,
         // another for its top edge, and another for its right edge:
         Binding areaLeft = new Binding( Edge.LEFT,
                                         12,
                                         Direction.RIGHT,
                                         Edge.LEFT,
                                         frame);
         Binding areaTop = new Binding(  Edge.TOP,
                                         12,
                                         Direction.BELOW,
                                         Edge.TOP,
                                         frame);
         Binding areaRight = new Binding(Edge.RIGHT,
                                         12,
                                         Direction.LEFT,
                                         Edge.RIGHT,
                                         frame);
                      
         // Now, create a RelativeConstraints object and add the bindings to it:
         RelativeConstraints areaConstraints = new RelativeConstraints();
         areaConstraints.addBindings(areaLeft, areaTop, areaRight);
         
         // Lastly, add the area to the frame with the constraints:
         frame.add(area, areaConstraints);
                      
         frame.setVisible(true);
 
The area component above will now always keep a 12 pixel margin on both the left and right, regardless of how big the window is.

Note that when using Bindings to make components dynamically resize, it is important which component is named in the Binding constructor and which is added to the frame with the constraint; the "floating" component (the component added to the frame with the constraint) will change size due to changes in the "fixed" component (the component named in the Binding constructor), but not vice versa.

Author:
Brian Ellis (firebird@andrew.cmu.edu)

Constructor Summary
Binding(Edge myEdge, int distance, Direction direction, Edge fixedEdge, Component fixedComponent)
          Constructs a Binding given the Edge of a "floating" component, a distance, a Direction, the Edge of a "fixed" component, and the fixed component itself.
Binding(RelativePosition relativePosition, Component fixedComponent)
          Constructs a Binding given a RelativePosition and a fixed component to resolve that position against.
 
Method Summary
 Object clone()
           
 Component getFixedComponent()
          Returns the fixed component for this Binding.
 RelativePosition getRelativePosition()
          Returns a RelativePosition object describing the position in this Binding.
 boolean isValid()
          Returns a boolean indicating whether this Binding is "valid"; that is, whether the relationship it describes is possible.
 void setFixedComponent(Component aFixedComponent)
          Sets the fixed component associated with this Binding.
 String toString()
          Returns a human-readable String representation of this Binding.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Binding

public Binding(Edge myEdge,
               int distance,
               Direction direction,
               Edge fixedEdge,
               Component fixedComponent)
Constructs a Binding given the Edge of a "floating" component, a distance, a Direction, the Edge of a "fixed" component, and the fixed component itself. The parameters are described below; for examples of use, see the class documentation for Binding.

Parameters:
myEdge - The edge of the "floating" component that is to be bound.
distance - The distance from the bound edge of the floating component to the fixed edge of the fixed component, in pixels.
direction - The direction of a line drawn from the fixed component's edge to the floating component's edge.
fixedEdge - The edge of the fixed component to which myEdge of the floating component will be bound.
fixedComponent - The component to which the floating component will be bound.

Binding

public Binding(RelativePosition relativePosition,
               Component fixedComponent)
Constructs a Binding given a RelativePosition and a fixed component to resolve that position against.

Parameters:
fixedComponent -
relativePosition -
Method Detail

clone

public Object clone()
Overrides:
clone in class Object
See Also:
Object.clone()

getFixedComponent

public Component getFixedComponent()
Returns the fixed component for this Binding.

Returns:
the value of fixedComponent

getRelativePosition

public RelativePosition getRelativePosition()
Returns a RelativePosition object describing the position in this Binding. This may have been passed in using the Binding(RelativePosition, Component) constructor, or may have been generated on the fly in the Binding(Edge, int, Direction, Edge, Component) constructor.

Returns:
the value of relativePosition

isValid

public boolean isValid()
Returns a boolean indicating whether this Binding is "valid"; that is, whether the relationship it describes is possible.

Returns:
true if the Binding is valid, false otherwise.
See Also:
RelativePosition.isValid()

setFixedComponent

public void setFixedComponent(Component aFixedComponent)
Sets the fixed component associated with this Binding. Since Bindings are copied when they are added to a RelativeConstraints object, you can change the fixed component of a Binding after adding it to a RelativeConstraints object and reuse the Binding that way.

Parameters:
aFixedComponent - The new fixed component.

toString

public String toString()
Returns a human-readable String representation of this Binding. Useful for troubleshooting and testing.

Overrides:
toString in class Object