6

I'm working on a project that use scala 2.9.2 and java 7.

What I'm trying to do is create a GUI using the scala ListView.

Here's a small code snippet:

private val listView = new ListView[Document](someList)
.
.
.
for (selectedDocument <- listView.peer.getSelectedValuesList) {
    doSomething(selectedDocument)
}

This gives me the following compile error:

error: something is wrong (wrong class file?): class JList with type parameters [E] gets applied to arguments [], phase = namer for (selectedDocument <- listView.peer.getSelectedValuesList) {

I'm guessing this is because in ListView, peer is defined without type parameter:

override lazy val peer: JList = new JList with SuperMixin

So the question is: is it impossible to use the ListView from scala-swing with Java 7?

ulejon
  • 631
  • 5
  • 22

3 Answers3

3

Solved this issue by extending Scala's ListView and adding a "typed peer".

class ExtendedListView[A: ClassManifest] extends ListView[A] {
    lazy val typedPeer: JList[A] = peer.asInstanceOf[JList[A]]

    def selectionEmpty = typedPeer.isSelectionEmpty

    // Other functions omitted
}

Works great!

ulejon
  • 631
  • 5
  • 22
1

The problem is that many swing components have been "generified" in Java 7, leading to incompatibilities. The scala library was certainly compiled toward a version of Java < 1.7. I would advise to compile your scala code against Java 1.6. You can still compile you Java code in Java 1.7 and use it from your scala code, and in additioncode compiled against Java 1.6 can run on a JVM 1.7.

UPDATE: someone stumbled against the same problem: http://comments.gmane.org/gmane.comp.lang.scala.debate/9158

Régis Jean-Gilles
  • 32,541
  • 5
  • 83
  • 97
  • To bad this hasn't been fixed in Scala yet. Using the Listview in my app would require my users to have jre6, or am I missing something? – ulejon Nov 06 '12 at 17:52
  • As far as I know, this is incorrect. Code compiled against Java 1.6 can very well be run on a JVM version 1.7 (it's the other way around that won't work). – Régis Jean-Gilles Nov 06 '12 at 17:56
0

I have written a small library SwingPlus which makes it possible to use a ListView both when compiling on Java 6 and Java 7+. The problem (as others have pointed out) is the generification of Swing in Java 7.

0__
  • 66,707
  • 21
  • 171
  • 266