0

JavaFX is a Framework for UIs. Nevertheless I would like to use JavaFX ObservableValue for non-UIs (or at least the way). Is this a good idea?! Personally I don't think so (UI-Thread, no concurrency).

In my projects it's pretty common to observe the properties of non-UI-related objects (backend). My current solution is to use an EventBus approach. It's work but when it comes to the UI it feel like a hack to do the databinding.

Example:

private void init () {
    myObject.subscribe(this);
    textfield.textProperty().addChangeListener( (o,l,n) -> {
        myObject.setText(n);
    });
}

@Handler
private void handleChangeEvent(final ChangeEvent event) {
    Platform.runLater(() -> this.textfield.setText(event.getValue()));
}

This Example becomes quite complex when the object is retrieved by the selected item of a ListView.

What I want:

private void init () {
    myObject.textProperty().bindBidirectional(textfield.textProperty());
}
Marcel Jaeschke
  • 707
  • 7
  • 24

1 Answers1

1

JavaFX may be considered as a UI framework, but JavaFX Properties are not made to be used by the JavaFX application alone.

You don't have to be on a JavaFX Application thread to use JavaFX Properties.

So, yes it is a good idea.

I would advise you to go through these threads:

JewelSea say's in his answer :

JavaFX properties are designed such that you don't need to be running a JavaFX program to use them

Community
  • 1
  • 1
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
  • 4
    All this is correct; however be aware that JavaFX properties are not designed to be thread safe, so without some kind of external synchronization they should only be used on a single thread. In practice, this would probably prevent the use of `bindBidirectional` between a JavaFX property in the backend (which is likely to be updated from a background thread) and a property bound to a UI control. At best you would have listeners invoking `Platform.runLater(...)` to update the UI property. – James_D Jan 09 '15 at 13:55
  • Is there a vanilla JDK 11+ package that provides similar functionality as JavaFX Observables, Properties, etc. provided in vanilla JDK 10 and earlier? All JavaFX content was dropped from the JDK in JDK 11, and it seems odd to add OpenJFX as a dependency exclusively for the aforementioned features. – TacoManStan Sep 11 '21 at 22:30