尝试在Scala中覆盖getListCellRenderComponent方法时出现对象创建不可能的错误 [英] Object creation impossible error when trying to override getListCellRenderComponent method in scala

查看:218
本文介绍了尝试在Scala中覆盖getListCellRenderComponent方法时出现对象创建不可能的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在scala类的DefaultListCellRenderer类中重写getListCellRendererComponent方法(我正在使用intellij scala插件)​​.这是下面的代码:

I'm trying to override the getListCellRendererComponent method in the DefaultListCellRenderer class in a scala class (I'm using the intellij scala plugin). Here's the code below:

val cellRenderer = new javax.swing.DefaultListCellRenderer {
  override def getListCellRendererComponent(list: JList[_], value: AnyRef, index: Int, isSelected: Boolean, cellHasFocus: Boolean): Component = {
    val retval: JLabel = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus).asInstanceOf[JLabel]
    retval.setForeground(getVariableColor(value.toString))
    return retval.asInstanceOf[JLabel]
  }
}

varsCombo.setRenderer(cellRenderer)

我得到这个错误: 无法创建对象,因为特征

I'm getthing this error: object creation impossible, since method getListCellRendererComponent in trait

类型为(x $ 1:javax.swing.JList [_< ;: Object],x $ 2: Object,x $ 3:Int,x $ 4:Boolean,x $ 5:Boolean)java.awt.Component不是 定义的 val cellRenderer = new javax.swing.DefaultListCellRenderer {

ListCellRenderer of type (x$1: javax.swing.JList[_ <: Object], x$2: Object, x$3: Int, x$4: Boolean, x$5: Boolean)java.awt.Component is not defined val cellRenderer = new javax.swing.DefaultListCellRenderer {

我对为什么感到非常困惑.我刚刚开始学习scala语言,无法想出为什么它不起作用的任何原因.

and I'm quite puzzled as to why.. I just started learning the scala language and can't think of any reason why this shouldn't work.

任何帮助表示感谢,谢谢!

Any help appreciated, thanks!

我找到了一个页面,其中某个人似乎也有相同的问题: http://www.scala-lang.org/old/node/10687

I found a page in which someone seems to have the same issue: http://www.scala-lang.org/old/node/10687

最后,我能够通过一个简单的解决方法来解决该问题.这是 扩展DefaultListCellRenderer的解决方案(任务是我的 域类):

"Finally I was able to solve the problem with a simple workaround. Here's the solution for extending the DefaultListCellRenderer (Task is my domain class):

object TaskCellRenderer extends ListCellRenderer[Task] {

 val peerRenderer: ListCellRenderer[Task] = (new
 DefaultListCellRenderer).asInstanceOf[ListCellRenderer[Task]]

 override def getListCellRendererComponent (
 list: JList[_ <: Task], task: Task, index: Int,
 isSelected: Boolean, cellHasFocus: Boolean): Component = {

val component = peerRenderer.getListCellRendererComponent(
 list, task, index, isSelected, cellHasFocus)
 .asInstanceOf[JComponent]

 // ... do some component customization here ...

component
}
}

我实现的不是直接扩展DefaultListCellRenderer ListCellRenderer接口. "

Instead of extending the DefaultListCellRenderer directly I implement the ListCellRenderer interface. "

问题是我不知道为什么这可以解决问题?以及如何将解决方案应用于我的问题..因为我们正在尝试实现略有不同的事情.

The problem is I don't know why this solves the issue? And how to apply the solution to my problem.. because we were trying to achieve slightly different things.

如果有人能解释该解决方案为什么起作用,或如何将其应用于我的问题..将不胜感激! (对不起,请记住,我是scala的新手)

If anyone can explain why the solution works, or how to apply it to my problem.. would be much appreciated! (sorry please keep in mind I am very new to scala)

推荐答案

您要覆盖Java通用方法:

You are overriding Java generic method:

public Component getListCellRendererComponent(
        JList<?> list,
        Object value,
        int index,
        boolean isSelected,
        boolean cellHasFocus)

这样做有局限性.请查看此以获取更多详细信息: Scala:覆盖通用Java方法II .

There are limitation on doing that. Please see this for more details: Scala: Overriding Generic Java Methods II.

这篇关于尝试在Scala中覆盖getListCellRenderComponent方法时出现对象创建不可能的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆