ISU570 - Spring 2009 - Futrelle - Application Programming

Professor Futrelle - College of Computer and Information Sciences, Northeastern U., Boston, MA

Version of January 3, 2009


Overview of the programming presented here - Java Swing

There are a variety of languages, IDEs, and systems that allow you to create interactive applications. I will concentrate on Java Swing, a facility I have used for years and am therefor reasonably familiar with. I can more efficiently illustrate interactive system development for you by focusing on Swing. Java was invented to create applets in browsers, so it had graphics from day one, as well as the ability to download and run code in browsers. The graphics evolved into Swing so it is possible to build substantial applications in Swing. Swing is a fine way to experiment with interactive paradigms without being drawn into the full complexities of developing for a particular OS. Professor Futrelle and his students have used Swing to build GUIs for a number of applications. He has had few if any problems developing code that runs the same way on Mac, Linux, and Windows.

Getting Java on Your Machine - Swing comes with it

You need to have Java on your machine - having Java 6 makes your life a bit simpler, as I'll explain, but all the generally available demos work just fine in Java 5 and even Java 4. If you go to a console or terminal window and type
java -version
you'll either get a version number or an error, meaning Java is installed or not. For example, on my Mac and Windows respectively, I get:

Mac:

~: java -version
java version "1.5.0_07"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_07-164)
Java HotSpot(TM) Client VM (build 1.5.0_07-87, mixed mode, sharing)

and

Windows:

C:\Documents and Settings\Robert Futrelle>java -version
java version "1.6.0_02"
Java(TM) SE Runtime Environment (build 1.6.0_02-b06)
Java HotSpot(TM) Client VM (build 1.6.0_02-b06, mixed mode, sharing)

Because I run Windows XP under VMware Fusion on my MacBook, I can always check both these platforms.

If, for some reason, you don't have Java on your machine, it's easy enough to download and install it. Go to this site. Note that the Mac is in a different category. Since Apple has a proprietary interactive interface on their systems, Sun does not directly furnish a Java download for the Mac. But they do point you to the Apple site. It should be on your Mac, though you could conceivably have an old version needing updating.

Learning about Swing

There is a full and up-to-date tutorial on Sun's Java site, as well as thousands of pages of useful information about Swing on the web.

Running the Swing Demos

Sun has a collection of over 50 demos on this page that you can run. They offer you a number of options:

There's another omnibus demo that shows you all the different things available in Swing. This version runs in a browser and doesn't require Java 6.

To give you the flavor of all this, here's the intro screen for the SwingSet Demos. The toolbar at the top gives you a choice of fifteen different demos.

Here's how simple Swing code can be:

package components;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/* FrameDemo.java requires no other files. */
public class FrameDemo {
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("FrameDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel emptyLabel = new JLabel("");
        emptyLabel.setPreferredSize(new Dimension(175, 100));
        frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

Which produces this, when launched on Windows:


Go to ISU570 Spring 2009 home page. or RPF's Teaching Gateway or homepage