Course Notes‎ > ‎

Chapter 9 - Swing Lightweight Components


In the last few sections we built user interfaces using the AWT (Abstract Windowing Toolkit) classes. We will now examine the use of advanced components, building a user interface using the JFC (Java Foundation Classes) Swing API.

The Java Foundation Classes (JFCs) are named like the Microsoft Foundation Classes (MFCs) and are equivalent in nature. The JFC is a group of packages that provide GUI classes for Java applets/applications. The AWT is the foundation of JFC, but JFC includes APIs such as Swing, 2D API (for 2D graphics) and the Accessibility API (for ease of access for people with disabilities).

Swing is an ever expanding library of components used to build Graphical User Interfaces. The AWT components we discussed previously are heavyweight components, relying on the operating system to render them. Swing components are lightweight components, completely independent of any operating system. Heavyweight and lightweight components can be mixed, however I would not advise it as it sometimes leads to difficulties in repainting. We will still use the AWT layout managers in creating our Swing based applications. Traditionally the Swing API was downloaded separate to the core API (in Java 1.x) - now it is a part of the core API (as of Java 2).

Some of the features of Swing Components are:

  • They are Lightweight - Most Swing components are written in Java and so do not depend on the host operating system to draw them. Because of this we can develop complex visual components.

  • All Swing components support the Accessibility API. In addition we can add tool-tips to a Button to provide further description of its function.

  • Since the components are lightweight we can change the Look-And-Feel of an application - even during run-time.

  • All Swing components can have it's own border around it, allowing us to design advanced layouts.

We will use Swing to develop applications. The JDK (Java JDK 7+) comes packaged with various demonstration programs. If you look in the directory C:\Java\jdk1.7.0\demo\jfc you will see a collection of Swing Applications. 

The first one to run is in SwingSet2, where it is stored as SwingSet2.jar i.e. a Java Archive that contains all the .class files for the example. You can execute this example by typing java -jar SwingSet2.jar in this directory. If you have your windows environment set up correctly 

You will see an application as in Figure 9.1, “The Swing Set 2 Example 1" and Figure 9.2, “The Swing Set 2 Example 2”. You can see from these figures that there is a huge selection of Swing components that you can use. Note that you can select the "source code" tab to see how the code in the window was written. This can be very useful if you see a component that you would like to use in the same format.

Figure 9.1. The Swing Set 2 Example 1

Figure 9.2. The Swing Set 2 Example 2

These notes are copyright Dr. Derek Molloy, School of Electronic Engineering, Dublin City University, Ireland 2013-present. Please contact him directly before reproducing any of the content in any way.