简体   繁体   English

类型不匹配的组合框标量

[英]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.. 我正在将旧的scala swing应用程序从scala 2.7.7升级到2.9.3,并将jdk 1.6升级到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. 我可以使用旧设置运行该应用程序,并且在IntelliJ IDEA 12.1中为scala 2.9.3和jdk 1.7升级该应用程序几乎成功。

But I keep running into problems with a ComboBox. 但是我仍然遇到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: 首先,IntelliJ使用jdk 1.7发出以下警告:

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: 当我直接使用此文件编译源代码时,使用jdk 1.6和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 ) (例如,我尝试了这个stackoverflow问题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. 我不是原始代码的作者,并且在过去的两周里我才刚刚学习了scala和swing。

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 我终于从这个问题的灵感中解决了这个问题, 将Scala 2.9.2中的ListView与Java 7结合使用会产生编译错误

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 现在,我可以使用jdk 1.7在scala 2.9.3中成功编译并运行整个项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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