Getting into the 'swing' of Java GUI's:

Simple Graphical User Interface 5

This tutorial shows how to implement the swing libraries.

The 'swing' libraries are an extension of the Java awt graphical libraries. Rather than relying on the operating system graphical elements, the swing libraries draw all the GUI components in Java itself. As a reult the swing components are caleed 'light weight' components. This means little to the programmer, except that a swing GUI can look the same on any platform, or it can take on the look and feel of the host operating system. Also, the swing libraries are more fully featured than the awt in many cases and a number of drawing details (such as double buffering and window closing) are dealt with automatically in swing components.

Each swing class that is equivalent to an awt class has the same name but with a J in front. For example the swing version of the Frame class is the JFrame class.

Below is the GUI program we encountered in GUI tutorial 1, but this time using the swing library.

Source code.

Let's have a closer look.

import java.awt.*;
import java.awt.event.*;
import jm.JMC;
import jm.music.data.*;
import jm.util.*;
import javax.swing.*;

The swing classes as part of the javax (extended) group of packages.

public class SimpleGUI extends JFrame implements ActionListener, JMC{
//Any class which creates a GUI will usually extend the Frame class!!

//how many times is the button pressed? class variable

private int musicNumber = 0;
//declare the button as a class variable so it can
// be used by any method in this class

private
JButton composeBtn;
// simple main method called when the class in run
public static void main(String[] args) {
new SimpleGUI();
}

The swing version of the frame and button classes are used.

        // constructor         
public SimpleGUI() {
//give the window a name
super("A simple GUI interface for jMusic");

//add the button
composeBtn = new Button("Compose");
composeBtn.addActionListener(this);
this.
getContentPane().add(composeBtn);

//display the window
this.pack();
this.setVisible(true);
}

A JFrame is a more complex beast than the awt Frame which is why it provides us with more built-in functionality, but it also adds some complexity such as requiring us to add components to its 'content pane' rather than directly to the frame itself.

The show keyword has been replaced with setVisible() in line with the policy of using get and set method names as consistently as possible from java versions 1.1 onward.

	// Deal with the button click        
public void actionPerformed(ActionEvent ae){
if (ae.getSource() == composeBtn) {
makeMusic();
//update the counter of button presses
musicNumber++;
System.out.println("The button has been pressed " +
musicNumber + " times");
}
}

No changes to the event handling, the awt classes are still used for this.

        public void makeMusic() {
// rest of code here....
// see the source for the full music making method
// it is not important to this tutorial
//.....

//write a MIDI file to disk

//.....
}

Of course there is no change to the jMusic code either.

That wasn't so hard was it :)

When you run this code on your machine it should take on the default 'metal' look rather than the look of your native window system (unless you run a system such as Mac OS X which defaults to its own - more pretty - visual appearance).

The swing libraries are much more complete than the awt, with components such as the JTabbedPane and so on. We suggest you use the swing libraries for the most part, and check out a good book (or the Sun web site) to familiarise yourself with the swing components.