简体   繁体   中英

type mismatch combobox scala

I am in the process of upgrading an old scala swing application from scala 2.7.7 to 2.9.3 and jdk 1.6 to preferably jdk 1.7..

I am able to run the application with the old setup, and I have almost succeeded in upgrading the application for scala 2.9.3 and jdk 1.7 in IntelliJ IDEA 12.1.

But I keep running into problems with a ComboBox.

The troublemaker file:

package gui

import gui.model.scenario._

import swing._

object ScenarioComboBox {
  private val model = new javax.swing.DefaultComboBoxModel
}
class ScenarioComboBox extends ComboBox[MutableScenario](Seq(new MutableScenario("", Map()))) {
  import ScenarioComboBox.model
  peer.setModel(model)
  private val dataModel = model.asInstanceOf[javax.swing.DefaultComboBoxModel]

  def contents = {
    var list: List[MutableScenario] = Nil
    val size = dataModel.getSize
    (0 to size - 1).foreach { index =>
      list = list ::: List(dataModel.getElementAt(index).asInstanceOf[MutableScenario])
    }
    list
  }
  def contents_=(v: List[MutableScenario]) {
    dataModel.removeAllElements
    v map dataModel.addElement
  }

  def selectedItem = dataModel.getSelectedItem.asInstanceOf[MutableScenario] match {
    case null => None
    case s    => Some(s)
  }

  def selectedItem_=(v: Option[MutableScenario]) {
    v match {
      case Some(s) => dataModel setSelectedItem s
      case None    => dataModel setSelectedItem null
    }
    selection.publish(swing.event.SelectionChanged(this))
  }

  listenTo(selection)
}

For a start, with jdk 1.7 IntelliJ gives following warning:

Type mismatch, expected: ComboBoxModel[E], actual: DefaultComboBoxModel[Nothing]

at the line:

peer.setModel(model)

When I compile the source with this file as it is, then I get following error, both with jdk 1.6 and 1.7:

something is wrong (wrong class file?): class JComboBox with type parameters [E] gets applied to arguments [], phase = typer
  peer.setModel(model)
       ^

I seem to get type mismatch errors and/or being unable to make this piece of code compile no matter what I try to fix the combobox. (I have for instance tried out the ideas presented in this stackoverflow question Editing Combobox Scala )

I am not the author of the original code, and I have only just learned scala and swing during the last two weeks.

I feel like I've tried out a million things with this combobox without any luck..

I finally solved this problem with inspiration from this question Using ListView from Scala 2.9.2 with Java 7 gives compile error

the code now look like this:

...
object ScenarioComboBox {
  private val model = new javax.swing.DefaultComboBoxModel[MutableScenario]
}
class ScenarioComboBox extends ComboBox[MutableScenario](Seq(new MutableScenario("", Map()))) {
  import ScenarioComboBox.model
  lazy val typedPeer: JComboBox[MutableScenario] = peer.asInstanceOf[JComboBox[MutableScenario]]
  typedPeer.setModel(model)
...

And I am now able to successfully compile and run the whole project in scala 2.9.3 with jdk 1.7

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