Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extend scala.swing?

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.

like image 451
Daniel C. Sobral Avatar asked Jul 16 '09 21:07

Daniel C. Sobral


1 Answers

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.

like image 197
sanity Avatar answered Oct 30 '22 12:10

sanity