3

Using this code as an example I have written the following to track changes of a slider and put the result into the "speed: Int" variable:

speedSlider.valueProperty.addListener(new ChangeListener[Number] {
  @Override
  def changed(o: ObservableValue[_ <: Number], oldVal: Number, newVal: Number) {
    speed = newVal.intValue()
  }
})

But it causes an error:

wrong number of type arguments for scalafx.beans.value.ObservableValue, should be 2
  def changed(o: ObservableValue[_ <: Number], oldVal: Number, newVal: Number) {

If I change ObservableValue[_ <: Number] to ObservableValue[_ <: Number, _ <: Number] this error disappears but another emerges:

object creation impossible, since method changed in trait ChangeListener of type (x$1: javafx.beans.value.ObservableValue[_ <: Number], x$2: Number, x$3: Number)Unit is not defined
speedSlider.valueProperty.addListener(new ChangeListener[Number] {
                                          ^

Any ideas?

Update: I have resolved the errors by replacing ObservableValue (which was being resolved into the ScalaFX version which I don't really understand) with javafx.beans.value.ObservableValue. It compiles and throws no errors now, but still doesn't work - the code is never invoked.

Ivan
  • 63,011
  • 101
  • 250
  • 382
  • Incidentally, in _Scala_, you should prefer the **override** modifier over the **@Override** annotation. _Scala_ is not _Java_. I'm not even sure the latter will work as you expect if is actually required. – Mike Allen May 14 '14 at 04:13

2 Answers2

4

Looking at the docs I see you don't need to pass a ChangeListener but simply an anonymous function with the same signature as the onChange method

speedSlider.valueProperty.addListener{ (o: javafx.beans.value.ObservableValue[_ <: Number], oldVal: Number, newVal: Number) =>
  speed = newVal.intValue
}

Otherwise the method will expect a javafx.beans.value.ChangeListener

I hope this solves the issue.

pagoda_5b
  • 7,333
  • 1
  • 27
  • 40
  • 1
    Well. This works (while the ChangeListener version I've written does not) but only as soon as I specify the types: `(o, oldVal, newVal)` does not compile, I had to replace it with `(o: javafx.beans.value.ObservableValue[_ <: Number], oldVal: Number, newVal: Number)`. – Ivan May 04 '14 at 20:00
  • 1
    I suspected so, but being hard replying from the tablet I tried to be as concise as possible. – pagoda_5b May 05 '14 at 08:30
3

Just in case someone else encounters this problem. With Scala 3 the following works:

        rotate.statusProperty().addListener(new ChangeListener[Animation.Status]() {
            override def changed(observableValue: ObservableValue[ _ <: Animation.Status],
                                oldValue: Animation.Status, newValue: Animation.Status) = {
                text2.setText("Was - " + oldValue + ", Now - " + newValue)

            }
        })

Notice that the anonymous class must have the type parameter fully defined, but not the overridden method.

user2051561
  • 838
  • 1
  • 7
  • 21