Scala Swing example
        
	Here (below) is an example of Scala interacting with the java Swing GUI library, to put up a real silly GUI window.  It's neither a good example of Swing usage, nor a good example of how much more clearly Scala can express some things than Java can; it's more of a "how-to", to show a little of how scala interacts with Java libraries.  It works and does almost the same thing on Solaris (2.8), Windows XP, and Mac OS X (10.2.4).
Somewhat interesting tidbits:
- I renamed JButton to JSwJB upon import.
- The variable exitButton is initialized with an underscore, which tells scala to give it a "default initialization value".
- I use "this." a lot; I think it should be required.  It is not required in Scala, just as it is not required in Java.
- In the mkExitButton method, I omitted the outermost {curly} braces.  I'm still not sure that it's a good idea to make them omittable.
- In the sbwob.init method, the call to mkExitButton does not need "()".
- In the next line, the anonymous instance of the inner class "goAway" was created without requiring me to say "new goAway".  That's because I declared the goAway class to be a "case class".
- "goAway with foo" is how you say that class goAway implements the Java interface named foo.
//
// Translated into Scala from the jython version seen at
//   http://www.eros-os.org/pipermail/e-lang/2002-April/006351.html ,
// which just happened to be a simple example at the top of
// the Google heap for "jython swing simple".
//
import javax.swing.{ JButton => JSwJB };
class sbwob extends javax.swing.JFrame {
    var exitButton : JSwJB = _;
    var paintCalls = 0;
    var buttonPresses = 0;
    override def paint (g: java.awt.Graphics) = {
        paintCalls = paintCalls + 1;
        Console.println( "sbwob paint calls " + this.paintCalls );
        g.setColor(java.awt.Color.black);
        g.drawString("hello", 10, 10);
        g.fillOval(10, 10, 100, 100);
    }
    def mkExitButton = exitButton = new JSwJB( "Get me Out" );
    def init = {
        Console.println( "sbwob frame thing" );
        this.setTitle( "sbwob frame thing" );
        this.setSize(256, 256);
        mkExitButton;
        exitButton.addActionListener(goAway());
        this.getContentPane().add( exitButton );
    }
    // Inner class to handle button presses
    //   Will exit only after 6 or more repaints.
    case class goAway with java.awt.event.ActionListener {
        def actionPerformed ( e: java.awt.event.ActionEvent ) = {
            buttonPresses = buttonPresses + 1;
            if( sbwob.this.paintCalls > 5 ) {
                Console.println( "Button pressed " +
                    sbwob.this.buttonPresses + " times.  Exiting." );
                System.exit(0);
            }
        }
    }
}
object sbw_main {
    def main ( args : Array[String] ) = {
        var myhr = new sbwob;
        myhr.init;
        myhr.setVisible(true);
        Thread.sleep(20000);
        /*
        catch {
            // How can I catch just the InterruptedException?
            case Throwable =>
                Console.println( "20-second sleep interrupted." );
        }
        */
        var m: String =
            if( myhr.buttonPresses == 0 ) "No button pressed.";
            else   "Button pressed " + myhr.buttonPresses +
                   " times.  Exiting.";
        Console.println( m + "  Ran out of patience." );
        System.exit( 0 );
    }
}
Updated:  I removed all of the blank lines because Radio's
renderer insists on putting three blank lines wherever I had
a single blank line.  Yecch.  So I made a directory to hold
the examples I come up with as I learn the language.  It is
here:  http://dal.i.am/sw/scala-ex/ and this particular
example is http://dal.i.am/sw/scala-ex/sbw_db8.scala
Further update:  the comment at the start had the wrong
message number from the e-lang list.  Fixed now.
        
           12:43:32 AM  
             
           
           