Swing introduction  Swing components  Events and listeners  Text fields/labels  Layouts  JPanel and Box


Threads Database Profiling Regular expressions Random numbers Compression Exceptions C Equivalents in Java
 Comment on this article  List of Java topics

Swing: User Interfaces in Java

Java has a fairly rich framework for creating user interfaces "out of the box". That framework is called Swing. Before learning Swing, it's worth making sure that you really understand the following:

  • the basic concepts of object-oriented programming (e.g. the notion of classes, object creation, inheritance);
  • basic Java syntax: how to call methods, how to create loops etc;
  • interfaces, in the programming sense.

Why am I being so insistent? Essentially, because the Swing framework is highly object-oriented. It contains various classes that extend one another, and the notion of extending a base class to add required functionality to that class is key to user interface programming in Java (and indeed in other languages). Another key part of Swing programming is the notion of listeners: classes or components that "listen out for" a particular event happening such as a button being clicked. And listeners are generally implemented as Java interfaces (in the programming sense), so they're also an important notion to understand. We'll give a brief review of interfaces as we go along, but it helps if you've come across them before.

Introduction to the Swing and user interface packages

Many of the Swing classes representing windows, lists, buttons etc live in the javax.swing package and subpackages. The names of most Swing classes start with the letter J: JFrame, JPanel, JButton, JList etc. In general, if you're looking for the class that represents some visual component in Swing, its name will start with J.

A few classes also live in the java.awt package and subpackages. AWT stands for Abstract Window Toolkit and was in some sense the precursor to Swing. AWT provides a bare-bones way of creating a user interface, based directly on "native" user interface components of the operating system. Swing actually builds on AWT, and some AWT classes are still used directly in Swing programming. For example, many of the classes responsible for laying out user interface components lie inside the java.awt package, and many classes to do with handling events live in java.awt.event; the Graphics interface, which represents a graphics context and is key to rendering graphics inside a window or component, is also originally part of the AWT package.

Example: a first Swing application

By way of a vary basic example which we will extent on the next page, the following application displays a window:

import javax.swing.JFrame;

public class MyApp {
  public static void main(String[] args) {
    // Actually slightly incorrect from a threading point of view
    JFrame frame = new JFrame("My Swing app");
    frame.setSize(500, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}

The lines that arguably do most of the work are the ones in bold which construct the window, specifically an instance of JFrame, and then make it visible. We mentioned that user interface components are generally represented by classes inside the javax.swing package with names beginning with J. In this case, a "window with decorations" is strictly speaking called a frame, and represented by the JFrame class. So creating a window means creating an instance of JFrame. We construct one just like most Java objects: by calling its constructor. Note that, as with many constructors, most allow us to specify some common parameters: in this case, the title of the window.

The call to set the size of the window is hopefully self-explanatory once you see it; let us just comment for now that in general, you need to think about the size (and potentially layout) of all windows/components when you're programming with Swing. The call to setDefaultCloseOperation() is a shortcut we can use in the very simple case where we want our application to exit when the window is closed: Swing will dutifully call System.exit() for us. We'll see later that we can customise what happens when a window is closed.

This application as it stands actually has a threading problem that we're going to ignore for now. (If you're interested, see the section on Application startup code in the discussion of SwingUtilities.invokeLater().)

Next: events and listeners

As it stands, there's one major problem with this application: it doesn't do very much. To start "doing" things with Swing, we need to respond to events: mouse clicks, button clicks etc. So on the next page, we get started properly by looking at the concept of events and listeners.

After that, we will look at two other key types of user interface component: text fields and labels.

Other topics: Applets

A topic which in practice is related to Swing is that of writing Java Applets: the mini Java applications which you often see embedded in web pages (and in principle could be embedded in other contexts).


Unless otherwise stated, the Java programming articles and tutorials on this site are written by Neil Coffey. Suggestions are always welcome if you wish to suggest topics for Java tutorials or programming articles, or if you simply have a programming question that you would like to see answered on this site. Most topics will be considered. But in particular, the site aims to provide tutorials and information on topics that aren't well covered elsewhere, or on Java performance information that is poorly described or understood. Suggestions may be made via the Javamex blog (see the site's front page for details).
Copyright © Javamex UK 2009. All rights reserved.