09. Java GUI (Part 1)

What is a GUI Program?

  • A Graphical User Interface (GUI) program that uses graphics for input and output. The graphics may be drawn using lines, circles or rectangles or inserted existing graphics objects (for example, a button or check box).

Figure 9.1 Example of a GUI program output

  • So, why should we write GUI programs? Because they're much more user friendly (and fun) compared to the console programs we have been running so far!

Java GUI Packages

What does Java provide for GUI programming?

Java contains classes for each GUI component in the AWT (Abstract Windowing Toolkit) package and Swing package. There are three types of GUI classes:

  • container classes for holding UI components or other containers (for example, Frame or JPanel)
  • helper classes for specifying details (for example, Font, or Color)
  • UI component classes which represent particular user components (for example, Button, or JComboBox)

When the GUI program requires user input, Java provides Event classes so the program could handle events such as clicking on a button or selecting an item from a menu. Events will be described later in this lesson.

What's the difference between the AWT package and the Swing package?

  • AWT (java.awt) contains classes for "heavy-weight" components (containers and UI components) that directly control the hardware, so the look of AWT components may not the same on different computers.
  • The Java "Swing" package (javax.swing) provides a set of "lightweight" (all-Java language) components that, to the maximum degree possible, work the same on all platforms. Note that the Swing classes start with a J. Do NOT mix swing and non-swing GUI components (except the "helper" classes like Graphics, Font, and Color don't have Swing versions).

This notes are only an introduction to GUI programming, so only a few containers and components will be shown here.

Java GUI Classes

The Java GUI examples given in this lesson will contain the following container classes:

  • JFrame (in javax.swing): often the starting point of a GUI application (figure 9.2 is a JFrame with a title bar and minimize, close window, etc.)
  • JPanel (in javax.swing): ideal for packing other components and subpanels
    • JFrame and JPanel objects will call theadd method (to add components to the container) and setLayout (to change the layout of the container). These methods are inherited from their superclass, Component (an AWT class)
    • JApplet is a type of Panel, and will be discussed later

The Java GUI examples given in this lesson will contain one or more of the following GUI component classes:

  • JButton (in javax.swing): the user will click on the button to trigger an action (figure 9.2 shows a JButton with "Enter Cat" as its text, among 3 other JButtons)
  • JCheckBox (in javax.swing): the user will click on a check box to check or uncheck (toggles), usually used to turn on or off a program feature (will be shown later)
  • JComboBox (in javax.swing): a drop-down menu in which the user may select an item from the menu to select an option (will be shown later)
  • JLabel (in javax.swing): used for displaying text (figure 9.2 uses JLabels for "Pet's Name", "Pet's birthyear" and "# tricks OR indoor/outdoor")
  • JTextField (in javax.swing): usually allows the user to enter text, but may be used for text output, also (figure 9.1 shows 3 JTextfields to the right of the 3 JLabels)

Figure 9.2 Example of Java components displayed

Java Events

When the user clicks on a button or checks a checkbox, how do we get the Java program to handle those events? First, this is what happens when the user interacts with a GUI:

  • When the user does something to the GUI component object (for example, click on a button), an event is triggered.
  • Triggering the event creates an event object and the JVM (Java Virtual Machine) calls a particular event method in the event handler object (which was previously "registered" as an event "listener" or handler on the GUI component object).
  • When the event method is called, the method receives the event object (as a parameter)
  • The method called will "handle" the event, often using information received by calling event object methods, for example:
    • public Object getSource( ) - Returns the object on which the Event initially occurred.
    • public String toString( ) - Returns a String representation of this Event Object.

Figure 9.3 Example of Button Event

Java Event Handling

So, how does the JVM know what method to call, and which object is the event handler for the GUI component?

A Java GUI program has 3 parts to make it happen:

  1. In the class header of the event handler class, implement the listener interface that matches the event for the GUI component. This requires the programmer to find out which event is triggered for the GUI component. For example, if you're handling a button, the button will trigger an ActionEvent when the user clicks on it, so your handler class will implement ActionListener: (we'll discuss how to determine which event later)

public class MyEventClass

implements ActionListener {

  1. Where the GUI component object is instantiated (or where you have a reference to it), register the handler object as a listener to the GUI component (this may be in a class separate from the Event Handler class). The method will be named add___Listener, where the ___ will be the name of the event. For example, if the component will trigger an ActionEvent, call:


  1. In the Event Handler class, override the listener interface method(s). The method(s) will be different for each interface. For example, if the interface is ActionListener, the method will be:

public void actionPerformed(ActionEvent e){

...//code that reacts to the action...


Event Classes and Listener Interfaces

  • All event classes in Java are subclasses of java.util.EventObject, which is where the getSource() and toString() methods are defined
  • The subpackage that contains all of the GUI programming events are in java.awt.event (so you should import java.awt.event.*; ), for example, ActionEvent, AdjustmentEvent, ItemEvent, KeyEvent, MouseEvent.
  • Event listener interfaces that correspond to particular event classes are also in the java.awt.event subpackage, for example, ActionListener, AdjustmentListener,ItemListener, KeyListener, MouseListener.

How do you find out which event(s) a particular GUI component triggers? There are several ways, for example, look it up in a Java reference book. Here is a table of events for many of othe GUI components (AWT and Swing):

*Note that some components may trigger more than one type of event. Which one to use depends on the type of action or variation of the component.

Once you determine which event a GUI component triggers, the part of your Java program that handles it will need to implement the corresponding listener interface. For example, if you have a JCheckBox in your program, you could handle an ItemEvent on it by including the 3 parts mentioned on the previous page (see the following table to determine Part 1).

The corresponding listener interfaces and their methods are:

NOTE: If you're interested in lower level events, such as Key events (typing on the computer keyboard), Mouse events, or Window events, see the optional page Low Level Events here.

TRY ON YOUR OWN: Write down the 3 parts of code for a JCheckBox, handling an ItemEvent for it. Assume the JCheckBox is declared JCheckBox checkBox=new JCheckBox(checkBoxName); (for part 2).


The 3 parts to put into your code to handle a JCheckBox event:
1. class CheckBoxHandler implements ItemListener { ...
2. Register a CheckBoxHandler object as a listener on the checkBox: 
3. Inside the CheckBoxHandler class (see links to examples below), override the ItemListener method:
                public void itemStateChanged( ItemEvent e){ 
                              // do some action based on the ItemEvent