Object creation impossible error when trying to override getListCellRenderComponent method in scala

懵懂的女人 提交于 2020-01-05 04:48:28

问题


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

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 {

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!

Edit:

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

"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
}
}

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.

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)


回答1:


You are overriding Java generic method:

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

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



来源:https://stackoverflow.com/questions/21247126/object-creation-impossible-error-when-trying-to-override-getlistcellrendercompon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!