I have a table column errorFixed
of type TableColumn[Error, Boolean]
inside a TableView[Error]
. My Error
class has a val fixed: Boolean
which I try to put into this table column.
I tried
errorFixed.cellValueFactory = features =>
ReadOnlyBooleanWrapper(features.value.fixed)
but it fails with
type mismatch;
found : scalafx.beans.property.ReadOnlyBooleanWrapper
required: scalafx.beans.value.ObservableValue[Boolean,Boolean]
which I really don't understand as ObservableValue[Boolean,Boolean]
is a supertype of ReadOnlyBooleanWrapper
according to the documentation.
If I cast it myself using .asInstanceOf[ObservableValue[Boolean, Boolean]]
it seems to work. What is going on here?
Short answer is: instead of
errorFixed.cellValueFactory = features =>
ReadOnlyBooleanWrapper(features.value.fixed)
you should use
errorFixed.cellValueFactory = features =>
ObjectProperty[Boolean](features.value.fixed)
or ReadOnlyObjectWrapper[Boolean]
.
A brief version of long answer: there are certain "frictions" between Scala and Java when working with primitive Java types, like boolean or int. This inconvenience shows up in property binding in ScalaFX. Not everything is inherited in an intuitive way. In this case
ReadOnlyBooleanWrapper
is a subclass of
ObservableValue[scala.Boolean, java.lang.Boolean]
but scala.Boolean
is not a subclass of java.lang.Boolean
which internally, in ScalaFX this leads to complications. Interesting thing is that the casting .asInstanceOf[ObservableValue[scala.Boolean, scala.Boolean]]
works, though type parameters do not match at compile time.
Thanks for positing a full code example (gist) this really helps in clarifying the question.