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:
We will use Swing to develop applications. The JDK (Java JDK 7+) comes packaged with various demonstration programs. If you look in the directory
The first one to run is in SwingSet2, where it is stored as
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.
Course Notes >