Table of Contents
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 Accesiblity 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 tooltips 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. Please note that you can actually develop Applets with the Swing set, but also note that you need a Java 2 compatible web browser to display these applets (such as Internet Explorer 5+).
The JDK (Java JDK 6) comes packaged with various demonstration programs. If you look in the directory
C:\j2sdk1.4.X.X\demo\jfc\SwingSet2 you will see a demonstration Swing Application. It is stored
SwingSet2.jar i.e. a Java Archive that contains all the
for the example. You can execute this example by typing java -jar SwingSet2.jar in this
directory. You will see an application as in Figure 12.1, “The Swing Set 2 Example 1 Application” and
Figure 12.2, “The Swing Set 2 Example 2 Application”. 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
© 2006 Dr. Derek Molloy (DCU).