4

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?

Gist with stripped down project to reproduce

mgttlinger
  • 1,435
  • 2
  • 21
  • 35
  • Try to use `(x: ObservableValue[Boolean,Boolean])` instead of `x.asInstanceOf[ObservableValue[Boolean, Boolean]]`. If it works, then it looks like a bug in compiler. – senia Feb 10 '14 at 08:08
  • @senia Gives me exactly the same error – mgttlinger Feb 10 '14 at 08:09
  • Have you tried `sbt clean`? Is there any chance that `Boolean` in `ObservableValue[Boolean,Boolean]` is `java.lang.Boolean`? I would also try `((((x: BooleanProperty): Property[Boolean, Boolean]): ReadOnlyProperty[Boolean, Boolean]): ObservableValue[Boolean, Boolean])` just in case. – senia Feb 10 '14 at 08:16
  • @senia `sbt clean` does not change the situation. The type is not the java `Boolean` or at least it should not as I have specified the scala `Boolean` in the type of my `TableColumn` and [according to the documentation](http://docs.scalafx.googlecode.com/hg/scalafx-1.0/scaladoc/index.html#scalafx.scene.control.TableColumn) the `cellValueFactory_=` method should expect `=> ObservableValue` of that type. Your suggestion with the multiple type _"steps"_ does not solve the issue either. – mgttlinger Feb 10 '14 at 08:26
  • Multiple "steps" is not for issue solving. It's for error localization. What is the error message for "multiple steps"? – senia Feb 10 '14 at 08:29
  • @senia Exactly the same as stated in the question. The error position arrow ("^") points at the first ":" where you specify that it should be of type `BooleanProperty`. – mgttlinger Feb 10 '14 at 08:32
  • It's an interesting issue, but I can't reproduce it. Could you create a minimal example? – senia Feb 10 '14 at 08:45
  • @senia [Here is a gist](https://gist.github.com/mgttlinger/943e238cf7b6ab2b631a) with all the files necessary to reproduce the error. You just need to have sbt installed and put them in the correct folder structure. If I `sbt run` it the error occurs. If I remove the comment to include the cast it works as expected. – mgttlinger Feb 10 '14 at 09:11
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/47166/discussion-between-mgttlinger-and-senia) – mgttlinger Feb 10 '14 at 11:59

1 Answers1

3

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.

Jarek
  • 1,513
  • 9
  • 16