Search code examples
swingscalaparameterscastingjcombobox

class JComboBox takes type parameters


I want to get value of instance of JComboBox in my listener:

object NoteListener extends ActionListener {
  def actionPerformed(e:ActionEvent):Unit = {
    println("Source: " + e.getSource.asInstanceOf[JComboBox].getValue)
  }
}

And I get this error:

[error] .../test.scala:30:  class JComboBox takes type parameters
[error] println("Source: " + e.getSource.asInstanceOf[JComboBox].getValue)

when I try to pass any parameter:

[error] .../test.scala:30: ']' expected but '(' found.
[error] println("Source: " + e.getSource.asInstanceOf[JComboBox(Array)].getValue)

Is it a bug, or my ignorance?


Solution

  • Your type parameters are incorrect:

    e.getSource.asInstanceOf[JComboBox(Array)].getValue)
    

    should be

    e.getSource.asInstanceOf[JComboBox[Array]].getValue)
    

    Note the [Array]. This is how you specify type parameters in Scala.