|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectrelativelayout.Binding
public class Binding
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:
You can think of each Binding like an English sentence: the
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);
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:
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:
Binding rightOfLabel = new Binding(Edge.LEFT, 8, Direction.RIGHT, Edge.RIGHT, label);
RelativeConstraints fieldConstraints = new RelativeConstraints();
fieldConstraints.addBinding(rightOfLabel);
frame.add(field, fieldConstraints);
The
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);
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.
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 |
---|
public Binding(Edge myEdge, int distance, Direction direction, Edge fixedEdge, Component fixedComponent)
Binding
.
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.public Binding(RelativePosition relativePosition, Component fixedComponent)
RelativePosition
and a fixed component to resolve that
position against.
fixedComponent
- relativePosition
- Method Detail |
---|
public Object clone()
clone
in class Object
Object.clone()
public Component getFixedComponent()
public RelativePosition getRelativePosition()
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.
public boolean isValid()
true
if the Binding is valid, false
otherwise.RelativePosition.isValid()
public void setFixedComponent(Component aFixedComponent)
RelativeConstraints
object, you can change the fixed component of a
Binding after adding it to a RelativeConstraints
object and reuse the Binding that
way.
aFixedComponent
- The new fixed component.public String toString()
toString
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |