简体   繁体   中英

How to declare a property change listener in ScalaFX properly?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM