James Williams
LinkedInMastodonGithub

Flash for Swing Developers

Tags: Flash Swing

It doesn't take one a long time to realize that Swing sometimes gets a bum rap. Developers had no idea how to handle the GUI thread and were prone to unknowingly block it. Add to that the fact that the default widget set LookAndFeel left much to be desired gave users the impression that Java Swing was kludgy, slow, and ugly.

Be that as it may, Swing has been around for over ten years now and not even the much ballyhooed introduction of JavaFX Script will be enough to kill it. That one core reason while Griffon doesn't try to do away with Swing. It says, "I know it will be around and there is a lot of legacy code in it, so let's use it in a more efficient way." I was happily surprised to find that there is a project for Flash that allows you to use Swing conventions and classes to create user interfaces in Flash. You don't even need a commercial application (Adobe Flash CS4 and the like) to get started.

You'll need to install the Flex SDK and add it to your classpath. Grab AsWing libraries and extract the swc file into your working directory.

Create the following file as hello.as

package { import org.aswing.*; import org.aswing.geom.IntDimension;
import flash.display.*; import flash.text.*;

public class hello extends Sprite {
    public function hello() {
        var frame:JFrame = new JFrame(this, "HelloWorld");            
        frame.setSize(new IntDimension(200,120));
        var label:JLabel = new JLabel("Hello, World!");

        frame.getContentPane().append(label);            
        addChild(frame);

        frame.show();
    }
}

}

From the command-line run:

mxmlc -include-libraries=AsWingA3.swc hello.as

That should create a swf file in the same directory that, when run in the browser shows a frame with a label that says "Hello, World!". The above code should seem very familiar to Swing developers. The only key differences are the lines adding the frame and showing the frame.

In AsWing and in regular Flash, the top-level element is the Stage. addChild(frame) adds our top-level element for user interaction to that Stage.

frame.show() is deprecated in conventional Java but in Groovy, it acts as an alias to frame.setVisible(true). Of minor mention is the frame.getContentPane().append(label) line. Actionscript uses append instead of add that Swing prefers.