views:

830

answers:

5

In response to a previous question on how to achieve a certain effect with Swing, I was directed to JDesktopPane and JInternalFrame. Unfortunately, scala.swing doesn't seem to have any wrapper for either class, so I'm left with extending it.

What do I have to know and do to make minimally usable wrappers for these classes, to be used with and by scala.swing, and what would be the additional steps to make most of them?

Edit:

As suggested by someone, let me explain the effect I intend to achieve. My program controls (personal) lottery bets. So I have a number of different tickets, each of which can have a number of different bets, and varying validities.

The idea is displaying each of these tickets in a separate "space", and the JInternalFrames seems to be just what I want, and letting people create new tickets, load them from files, save them to files, and generally checking or editing the information in each.

Besides that, there needs to be a space to display the lottery results, and I intend to evolve the program to be able to control collective bets -- who contributed with how much, and how any winning should be split. I haven't considered the interface for that yet.

Please note that:

  • I can't "just use" the Java classes, and still take full advantage of Scala swing features. The answers in the previous question already tell me how to do what I want with the Java classes, and that is not what I'm asking here.

  • Reading the source code of existing scala.swing classes to learn how to do it is the work I'm trying to avoid with this question.

A: 

You can import all java libraries directly into your scala code.

Try the scala tutorial section: "interaction with Java".

Java in scala

Wolfslack
A: 

You might be be able to use the scala.swing source as reference e.g. http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk/src/swing/scala/swing/Button.scala

thatismatt
A: 

What sort of scala features are you trying to use with it? That might help in coming up with with an answer. I.e. - what is it you're trying to do with it, potentially in Java? Then we can try to come up with a nicer way to do it with Scala and/or create a wrapper for the classes which would make what you're trying to do even easier.

Antony Stubbs
Fair enough. I'll ammend my question.
Daniel
Thanks for the extra info, but I meant at a more technical level - i.e. programatically, what do you want to do with the Swing class?
Antony Stubbs
+3  A: 

You might consider Scala's "implicit conversions" mechanism. You could do something like this:

implicit def enrichJInternalFrame(ji : JInternalFrame) = 
               new RichJInternalFrame(ji)

You now define a class RichJInternalFrame() which takes a JInternalFrame, and has whatever methods you'd like to extend JInternalFrame with, eg:

class RichJInternalFrame(wrapped : JInternalFrame) {
   def showThis = {
     wrapped.show()
   }
}

This creates a new method showThis which just calls show on the JInternalFrame. You could now call this method on a JInternalFrame:

val jif = new JInternalFrame()
println(jif.showThis);

Scala will automatically convert jif into a RichJInternalFrame and let you call this method on it.

sanity
Perhaps I should have been more explicit in the description of my question. As the question itself says, I want to extend scala.swing. How do I extend *scala.swing*? An answer which doesn't even mention a single trait, class or method of scala.swing can't possible answer the question!
Daniel
A: 

In JRuby, you could mix in one (or more) traits into JDesktopPane or JInternalFrame instead of extending them. This way you wouldn't have to wrap the classes but just use the existing objects. As far as I know, this is not possible with Scala traits.

Luckily, there is a solution, almost as flexible as Ruby's: lexically open classes. This blog article gives an excellent introduction, IMHO.

bb