Home > Articles

Getting Started with Swing

  • Print
  • + Share This
In this sample chapter, you'll get a brief introduction to using the Java Foundation Classes (JFC) Swing packages, you'll find out how to get your hands on the right software, and you'll be walked through the process of compiling and running a program that uses the Swing packages.
This chapter is from the book

This chapter gives you a brief introduction to using the JavaTM Foundation Classes (JFC) Swing packages. After telling you about JFC and Swing, it helps you get the necessary software and walks you through how to compile and run a program that uses the Swing packages. Next, it shows you how to run programs using Java Web Start.

The next chapter, Learning Swing by Example (page 11), will build on these first steps to help you create several increasingly more complex examples. For now, let's start with the basics.

  • About the JFC and Swing

    • Which Releases Contain the Swing API?

    • Which Swing Packages Should I Use?

  • Compiling and Running Swing Programs

    • Install the Latest Release of the Java 2 Platform

    • Create a Program That Uses Swing Components

    • Compile the Program

    • Run the Program

    • Running Programs Using Java Web Start

  • Questions and Exercises

  • Example Programs

About the JFC and Swing

JFC is short for Java Foundation Classes, which encompass a group of features for building graphical user interfaces (GUIs1) and adding rich graphics functionality and interactivity to Java applications. JFC was first announced at the 1997 JavaOneSM developer conference. It is defined as containing the features shown in Table 1.2

Table 1. Features of the Java Foundation Classes



Swing GUI Components

Includes everything from buttons to split panes to tables. See screenshots of all the components in A Visual Index to Swing Components (page 37) in Chapter 3.

Pluggable Look-and-Feel Support

Gives any program that uses Swing components a choice of look and feel. For example, the same program can use either the Java or the Windows look and feel. Many more look-and-feel packages are available from various sources. As of v1.4.2, the Java platform supports the GTK+ look and feel, which makes hundreds of existing look and feels available to Swing programs.

Accessibility API

Enables assistive technologies, such as screen readers and Braille displays, to get information from the user interface.


Enables developers to easily incorporate high-quality 2D graphics, text, and images in applications and applets. Java 2D includes extensive APIs for generating and sending high-quality output to printing devices.

Drag-and-Drop Support

Provides the ability to drag and drop between Java applications and native applications.


Allows developers to build applications that can interact with users worldwide in their own languages and cultural conventions. With the input method framework developers can build applications that accept text in languages that use thousands of different characters, such as Japanese, Chinese, or Korean.

This book concentrates on the Swing components. We help you choose the appropriate components for your GUI, tell you how to use them, and give you the background information you need to use them effectively. We also discuss other JFC features as they apply to Swing components.

"Swing" was the code name of the project that developed the new components. Although unofficial, it's frequently used to refer to the new components and related API. "Swing" is immortalized in the package names for the Swing API, which begin with javax.swing.

Which Releases Contain the Swing API?

The short answer is that the Swing API has been included in the Java 2 platform, Standard Edition (J2SETM) since its initial release (1.2). A 1.4.2 release of the Java 2 platform is included on the CD that accompanies this book. You can also download the latest release from the Sun Microsystems Web site at: http://java.sun.com/j2se/.

This book concentrates on the Swing API in the Java 2 platform, Standard Edition, v1.4.2. Except where noted, the code in this book works, without change, with earlier J2SE releases.3 We also include notes about a few important changes expected in 1.5.

Which Swing Packages Should I Use?

The Swing API is powerful, flexible—and immense. In release 1.4 of the Java platform, the Swing API has 17 public packages:



















Fortunately, most programs use only a small subset of the API. This book sorts out the API for you, giving you examples of common code and pointing you to methods and classes you're likely to need. Most of the code in this book uses only one or two Swing packages:

  • javax.swing

  • javax.swing.event (not always required)

  • + Share This
  • 🔖 Save To Your Account