0

I have a Java method signature that I can't seem to convert to a signature in Java.

Here is the Java code:

public class InjectorListCellRenderer extends DefaultListCellRenderer {
  public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    throw new RuntimeException("not important");
  }  
}

And here the Scala code which I hoped would be equivalent:

class InjectorListCellRenderer(var painter: ParticleLabelPainter) extends DefaultListCellRenderer {
  override def getListCellRendererComponent(list: JList[_], value: Any, index: Int, isSelected: Boolean, cellHasFocus: Boolean): Component = {
    throw new RuntimeException("not important")
  }
}

However when compiling with sbt and Scala 2.11.8 I get the following error:

class InjectorListCellRenderer needs to be abstract, 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

The documentation for the base class DefaultListCellRenderer can be found here.

I can't seem to reproduce this problem with my own code.

KeyboardDrummer
  • 577
  • 5
  • 21

2 Answers2

3

Starting from the error message I would guess the following should work:

class InjectorListCellRenderer(var painter: ParticleLabelPainter) extends DefaultListCellRenderer {
  override def getListCellRendererComponent(list: JList[_ <: AnyRef], value: AnyRef, index: Int, isSelected: Boolean, cellHasFocus: Boolean): Component = {
    throw new RuntimeException("not important")
  }
}

Edit: after some experimentation I think that inheriting from DefaultListCellRenderer and overriding getListCellRendererComponent is not possible due to some inconsistency between Java's and Scala's type representations. If this is important to you you might consider filing a bug report.

Jasper-M
  • 14,966
  • 2
  • 26
  • 37
-1

Its saying the type of the JList argument isnt the same because you've left it as a hole [_] but it expects at least object.

Seems to work if you put object, any or make it parametric with T.

class InjectorListCellRenderer() extends DefaultListCellRenderer{
   def getListCellRendererComponent(list: JList[Object], value: Any, index: Int, isSelected: Boolean, cellHasFocus: Boolean) = ???
}

or

class InjectorListCellRenderer() extends DefaultListCellRenderer{
   def getListCellRendererComponent[T](list: JList[T], value: Any, index: Int, isSelected: Boolean, cellHasFocus: Boolean) = ???
}
Stephen
  • 4,228
  • 4
  • 29
  • 40
  • 1
    Did you actually try it? Unless I missed something, you should get an error about two methods with same erasure. And if it did, neither of these methods would override the Java method and thus wouldn't be called by the `JList` code. – Alexey Romanov May 20 '17 at 17:42
  • I've tried the proposed solution and it does not work. – KeyboardDrummer May 20 '17 at 21:12
  • To be clear those are two solutions, you cant use both at the same time (same class name). I ran both in the console and it compiled, but I dont have a full app using it. – Stephen May 21 '17 at 00:00