0

I'm using addChangeListener from the Java Swing API in my Scala program. The compiler of ScalaFiddle indicates an error in the following code :

spinner_1.addChangeListener(e => {
              ...
})

error: missing parameter type spinner_1.addChangeListener(e => {

I tried to write e : ChangeListener, but it didn't solve this problem. I didn't find something useful for that in Scala's documentation.

How could I deal with it ?

0

1 Answer 1

3

It works in Scala 2.12. But if you have to use older version like 2.11, you can use implicit conversion. Here is an example:

import javax.swing.JTabbedPane
import javax.swing.event.{ChangeEvent, ChangeListener}

implicit def convertToChangeListener(f: ChangeEvent => Unit): ChangeListener =
  new ChangeListener {
    override def stateChanged(e: ChangeEvent): Unit = f.apply(e)
  }

val listener: ChangeListener = (e: ChangeEvent)  => ...

new JTabbedPane().addChangeListener(listener)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.