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); ...