Skip to content

Instantly share code, notes, and snippets.

@s-leroux
Last active November 26, 2015 11:08
Show Gist options
  • Save s-leroux/761667db72f60b432abf to your computer and use it in GitHub Desktop.
Save s-leroux/761667db72f60b432abf to your computer and use it in GitHub Desktop.
Loony Corn JavaFX course on Udemy -- experiments with properties on lecture 18
package loonycorn.javafx;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Lecture18a extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
TextArea src1 = new TextArea();
TextArea src2 = new TextArea();
TextArea dst = new TextArea();
MyModel model = new MyModel();
// NOT WORKING AS EXPECTED:
// the second binding simply replace the first.
// only update of "src2" will update the "dst" text
model.simpleStringProperty().bind(src1.textProperty());
model.simpleStringProperty().bind(src2.textProperty());
dst.textProperty().bind(model.simpleStringProperty());
VBox root = new VBox(src1,src2,dst);
Scene scene = new Scene(root, 500, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
package loonycorn.javafx;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Lecture18a extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
TextArea src1 = new TextArea();
TextArea src2 = new TextArea();
TextArea dst = new TextArea();
MyModel model = new MyModel();
// WORKING AS EXPECTED:
// update of any "src" text area will overwrite the "dst" text
src1.textProperty().addListener(e -> model.setSimpleString(src1.getText()));
src2.textProperty().addListener(e -> model.setSimpleString(src2.getText()));
dst.textProperty().bind(model.simpleStringProperty());
VBox root = new VBox(src1,src2,dst);
Scene scene = new Scene(root, 500, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
package loonycorn.javafx;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Lecture18a extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
TextArea src1 = new TextArea();
TextArea src2 = new TextArea();
TextArea dst = new TextArea();
MyModel model = new MyModel();
// KEEP ALL IN SYNC:
// update of any "src" text area will overwrite the "dst" text and the other "src"
src1.textProperty().addListener(e -> model.setSimpleString(src1.getText()));
src1.textProperty().addListener((observable, oldValue, newValue) -> {
// prevent infinite recursion
if (!oldValue.equals(newValue)) {
src2.setText(newValue);
}
});
src2.textProperty().addListener(e -> model.setSimpleString(src2.getText()));
src2.textProperty().addListener((observable, oldValue, newValue) -> {
// prevent infinite recursion
if (!oldValue.equals(newValue)) {
src1.setText(newValue);
}
});
dst.textProperty().bind(model.simpleStringProperty());
VBox root = new VBox(src1,src2,dst);
Scene scene = new Scene(root, 500, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment