Binds in practice
Autor: Jean Paul Lopes | Publicado: 2011-07-25T06:18:00-07:00

One of the best features of JavaFX Script (in my opinion) was the operator "bind".

With the language's change in version 2.0 this operator was changed to a special type of data. But with great versatility.

As stated in the title of the post, we'll see it in practice. Below is an application that will demonstrate some of its applications.
public class Properties extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Application.launch(Properties.class, args);
}
private Rectangle rect1;
private Rectangle rect2;
private Rectangle rect3;
private TextBox edit1;
private TextBox edit2;

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World");
Group root = new Group();
Scene scene = new Scene(root, 300, 250, Color.LIGHTGREEN);
Button btn = new Button();
btn.setLayoutX(40);
btn.setLayoutY(130);
btn.setText("Rectangles");
btn.setOnAction(new EventHandler() {

public void handle(ActionEvent event) {
TranslateTransition tt = new TranslateTransition(Duration.valueOf(3000),rect1);
RotateTransition rt = new RotateTransition(Duration.valueOf(3000), rect1);
tt.toXProperty().set(600);
tt.toYProperty().set(500);
tt.cycleCountProperty().set(3);
rt.cycleCountProperty().set(3);
rt.setToAngle(720);
tt.autoReverseProperty().set(true);
tt.play();
rt.play();
}
});
Button btn1 = new Button();
btn1.setLayoutX(40);
btn1.setLayoutY(150);
btn1.setText("edits");
btn1.setOnAction(new EventHandler() {

public void handle(ActionEvent event) {
edit1.setLayoutX(100);
edit1.setLayoutY(200);
edit2.textProperty().bindBidirectional(edit1.textProperty());
}
});

rect1 = new Rectangle(100,100);
rect2 = new Rectangle(100,100);
rect3 = new Rectangle(200,150,100,100);
rect2.translateXProperty().bind(rect1.translateXProperty().add(rect1.widthProperty()).add(20));
rect2.layoutYProperty().bind(rect1.layoutYProperty());
rect2.rotateProperty().bind(rect1.rotateProperty().divide(2));
rect3.rotateProperty().bind(rect1.rotateProperty().multiply(2));

edit1 = new TextBox("edit1");
edit2 = new TextBox();

edit2.layoutXProperty().bind(edit1.layoutXProperty());
edit2.layoutYProperty().bind(edit1.layoutYProperty().add(edit1.heightProperty()).add(10));

root.getChildren().addAll(rect1,rect2,rect3,edit1,edit2);        
root.getChildren().addAll(btn,btn1);         
primaryStage.setScene(scene);
primaryStage.setVisible(true);
}
}



The application places at the window three rectangles. The first (rect1) receive position and rotation changes by Transition's (more about them at ).
The second rectangle will take their properties translateX, translateY and rotation accompanying object rect1 properties. The property  translateY will be binding directly, translateX will add to it also rect1 width, keeping the same distance between the two rectangles. Since the rotation property of the object rect2 monitor the same object rect1 property, but a rate of half of its "speed".
Instead, the object rect3 monitor the rotation of the object rect1, but with double his "speed".

Since the objects and edit1 edit2 will accompany the text property of each other through a "bindBidirectional." So with a single method the ability to add accompaniments bidirectional, where the changes made to reflect a property directly to another and vice versa.

These are simple examples but they demonstrate the capacity and capability of the classes that implements the Property.

Even more,