简体   繁体   中英

Object creation impossible error when trying to override getListCellRenderComponent method in 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

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)

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 .

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