调用一个接受`<! - ?的java方法?超级T - >`来自scala代码的类型参数?

时间:2014-05-08 05:01:41

标签: java scala types javafx scalafx

我正在编写一些scalafx代码,需要从java调用一个方法:

val txtEditor = new TextArea {
    text = "markdown here"
  }  

txtEditor.text.addListener(new ChangeListener[String] {
    override def stateChanged(e: ChangeEvent): Unit = ???
  })

但它报告了addListener参数的编译错误:

  

类型不匹配,预期:ChangeListener [_&gt;:String],         actual:ChangeListener with Object {def stateChange(e:ChangeEvent)Unit}

addListener是一个java方法:

void addListener(ChangeListener<? super T> listener);

如何解决?

1 个答案:

答案 0 :(得分:1)

FTR,回答问题标题。

scala> val is = (1 to 10).to[collection.mutable.LinkedList].asJava
warning: there were 2 deprecation warning(s); re-run with -deprecation for details
is: java.util.List[Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

scala> is sort (new java.util.Comparator[Int] { def compare(a: Int, b: Int) = b - a })

scala> is
res13: java.util.List[Int] = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]