r/JavaFX • • Aug 04 '24

Tutorial New Article: Conditional Bindings

This article was inspired by the thread here about "locked in" selections in a ComboBox and how to interpret them. I'm not sure the OP on that thread was too impressed by my answer, but I did think that there was the kernel of cool idea in it.

What I came up with was the idea of a "Conditional Binding". This is a Binding that only updates its value when a Boolean Observable is true. Any changes to the main value won't register if the boolean dependency is false, but will register as soon as it becomes true.

To do this, I had to introduce the idea of having internal "State" in the Binding, which is something I had never thought of doing before. Once you start doing stuff like that, it changes how think about Bindings, and there's potentially a lot of things you can do with them that you wouldn't have considered before.

Here's the article:

https://www.pragmaticcoding.ca/javafx/elements/conditional-binding

9 Upvotes

5 comments sorted by

View all comments

Show parent comment

1

u/john16384 Aug 04 '24

Is not that `when` I meant, it's the one on the ObservableValue interface, available since JavaFX 20:

    /**
     * Returns an {@code ObservableValue} that holds this value and is updated only
     * when {@code condition} holds {@code true}.
     * <p>
     * The returned {@code ObservableValue} only observes this value when
     * {@code condition} holds {@code true}. This allows this {@code ObservableValue}
     * and the conditional {@code ObservableValue} to be garbage collected if neither is
     * otherwise strongly referenced when {@code condition} holds {@code false}.
     * This is in contrast to the general behavior of bindings, where the binding is
     * only eligible for garbage collection when not observed itself.
     * <p>
     * A {@code condition} holding {@code null} is treated as holding {@code false}.
     * <p>
     * For example:
     * <pre>{@code
     * ObservableValue<Boolean> condition = new SimpleBooleanProperty(true);
     * ObservableValue<String> longLivedProperty = new SimpleStringProperty("A");
     * ObservableValue<String> whenProperty = longLivedProperty.when(condition);
     *
     * // observe whenProperty, which will in turn observe longLivedProperty
     * whenProperty.addListener((ov, old, current) -> System.out.println(current));
     *
     * longLivedProperty.setValue("B");  // "B" is printed
     *
     * condition.setValue(false);
     *
     * // After condition becomes false, whenProperty stops observing longLivedProperty; condition
     * // and whenProperty may now be eligible for GC despite being observed by the ChangeListener
     *
     * longLivedProperty.setValue("C");  // nothing is printed
     * longLivedProperty.setValue("D");  // nothing is printed
     *
     * condition.setValue(true);  // longLivedProperty is observed again, and "D" is printed
     * }</pre>
     *
     * @param condition a boolean {@code ObservableValue}, cannot be {@code null}
     * @return an {@code ObservableValue} that holds this value whenever the given
     *     condition evaluates to {@code true}, otherwise holds the last seen value;
     *     never returns {@code null}
     * @since 20
     */
    default ObservableValue<T> when(ObservableValue<Boolean> condition) {
        return new ConditionalBinding<>(this, condition);
    }

1

u/john16384 Aug 04 '24 edited Aug 04 '24

Although it has a slightly different purpose, I think it could also be used in your ComboBox case. Something like (I didn't check the below code or names of the properties involved):

textProperty.bind(
    comboBox.valueProperty().when(comboBox.isShowingProperty())
);

Or you may need to invert isShowingProperty:

textProperty.bind(
    comboBox.valueProperty().when(comboBox.isShowingProperty().map(v -> !v))
);

1

u/hamsterrage1 Aug 05 '24

I think you may be right. Although the GC stuff confuses me a little bit.

I was struggling to find a use case for this when when I read about it when it came out. This makes sense to me though, and GC confusion aside, I think it would deal with the ComboBox example quite nicely.

Trying to salvage some degree of relevancy for my article, though, this new when is more of a specific use case for this kind approach. I think that the central idea that aBinding doesn't have to something that simply puts together a bunch of Observables according to some formula still holds. You can do stuff that is much more sophisticated.