I have a situation where I want to bind BooleanProperty to a BooleanProperty ObservableList state wrapped inside ObjectProperty .
Here is a basic overview of the behavior I'm looking for:
ObjectProperty<ObservableList<String>> obp = new SimpleObjectProperty<ObservableList<String>>(); BooleanProperty hasStuff = new SimpleBooleanProperty(); hasStuff.bind();
I would like to use builders in the Bindings class instead of embedding a custom binding chain.
The Bindings.select(...) method theoretically does what I want, except that there is no Bindings.selectObservableCollection(...) and discarding the return value from the general select(...) and passing it to Bindings.isEmpty(...) does not work. That is the result:
hasStuff.bind(Bindings.isEmpty((ObservableList<String>) Bindings.select(obp, "value")));
throws a ClassCastException :
java.lang.ClassCastException: com.sun.javafx.binding.SelectBinding$AsObject cannot be cast to javafx.collections.ObservableList
Is it possible to use this use case only using the Bindings API?
Decision
Based on the answer from @fabian, here is the solution that worked:
ObjectProperty<ObservableList<String>> obp = new SimpleObjectProperty<ObservableList<String>>(); ListProperty<String> lstProp = new SimpleListProperty<>(); lstProp.bind(obp); BooleanProperty hasStuff = new SimpleBooleanProperty(); hasStuff.bind(not(lstProp.emptyProperty())); assertFalse(hasStuff.getValue()); obp.set(FXCollections.<String>observableArrayList()); assertFalse(hasStuff.getValue()); obp.get().add("Thing"); assertTrue(hasStuff.getValue()); obp.get().clear(); assertFalse(hasStuff.getValue());
java javafx javafx-2 binding observable
metasim
source share