Nemo_Sol Nemo_Sol - 12 days ago 5
Java Question

How to bind Borderpane left property to node with JavaFX

How would I go about changing the borderpane's left property to a certain node whenever an integer variable is changed? What type of ObjectProperty do I use and how do I bind this as well as listen to the integer variable?

Answer
BorderPane border = new BorderPane();
IntegerProperty intProp = new SimpleIntegerProperty();

border.leftProperty().bind(Bindings.createObjectProperty(
    () -> {
        int value = intProp.get();
        Node node = getLeftNodeForValue(value);
        return node ;
    },
    intProp));

Where getLeftNodeForValue(...) is just a method that retrieves the appropriate node for a give integer value.

Alternatively, just use a listener instead of a binding:

intProp.addListener((obs, oldValue, newValue) -> 
    border.setLeft(getLeftNodeForValue(newValue.intValue())));
Comments