Implementing base binding classes

The concept of binding is simple:

  • Add a listener to change events you want to track
  • Compute the value you want to have

Correspondingly, in the following example, we call bind() for the desired property and override computeValue() method:

    public void start(Stage primaryStage) {        Button btn = new Button();        btn.setText("Click me");        StackPane root = new StackPane();        root.setBackground(Background.EMPTY);        root.getChildren().add(btn);                Scene scene = new Scene(root, 300, 250);        ObjectBinding<Paint> objectBinding = new ObjectBinding<Paint>() {            {                bind(btn.pressedProperty());             }            @Override            protected Paint computeValue() {                return btn.isPressed() ? Color.RED : Color.GREEN;            }        };         scene.fillProperty().bind(objectBinding); ...

Get Mastering JavaFX 10 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.