[an error occurred while processing this directive]

Page One
Page Two
The Databank
What Is Swing?
Special Report
IDE Roundup
Swing and the Web
Swing Text
Tech Topics
Friends
Tips and Tricks
The PLAF Papers
Call 911
The Archive
JFC Home
Download Swing
Swing API Docs
Download JDK
JDK Docs
Java Tutorial
 
The Swing Connection What Is Swing?


What Is Swing? - 1
Getting Started with Swing

NOTE: This article has three parts. This part, "Getting Started with Swing," introduces the basic fundamentals of Swing and Swing technology. Part 2, "Introducing Swing Architecture," tells how Swing Components are designed. Part 3, "Creating a Swing Applet," presents a tutorial that you can follow to build your own applet using Swing.

You can find more "What Is Swing?" articles in The Swing Connection Archive.

By Mark Andrews

Swinging Couple (dancers)Now that the Java Foundation Classes (JFC) are officially part of the JDK core, more developers than ever are now going to be making the big leap to Swing. If you´ve been thinking about migrating to Swing but haven´t done so yet, you´re just the person that this article is written for.

In a nutshell, this article is a relatively short, highly concentrated crash course in Swing. It won´t teach you everything there is to know about Swing, but it lays out most of the important differences between yesterday´s Abstract Windowing Toolkit (AWT) and today´s Swing component set, and it concludes with a step-by-step tutorial that shows you how to create a simple animated applet using Swing.

This article is divided into three parts:

To help cut down on loading time, the first two parts of this article are presented on this page, and the third part -- a tutorial on creating an applet -- is presented on a separate page. You can jump immediately to the tutorial by clicking the "Creating an Applet with Swing" link (above), or by clicking this blue arrow:  arrow_blue_right

Horizontal rule

What Is Swing?

Duke SwingingSwing, as you may know, is a new GUI component kit that simplifies and streamlines the development of windowing components. Windowing components are the visual components (such as menus, tool bars, dialogs and the like) that are used in graphically based applets and applications.

The Swing component set is part of a new class library called the JavaTM Foundation Classes, or JFC. JFC. Swing makes up the bulk of JFC, and is available for download right now from the Java Software Web site. JFC also contains three other new utilities: the Java 2D, Drag and Drop, and Accessibility APIs.

Until Swing 1.0 was released last spring, Java-language programmers created GUIs for their applets and applications using the Abstract Windowing Toolkit, or AWT, which is a standard package provided with the Java programming language. Swing 1.0 was initially developed as a separate, downloadable library that could be used in conjunction with JDK 1.1.

The currently shipping release of Swing -- Version 1.1 -- is integrated into the current JDK release, which is named JDK 1.2.

When JDK 1.2 was officially released in December 1998, Swing and the rest of JFC officially became part of the JDK core. Older-style AWT components are still supported in JDK 1.2, and continue to work to work just fine. But Swing components are so much more powerful and elegant than their AWT counterparts that just about all Java-language developers are now migrating to Swing.

Horizontal rule 

Swing´s PL&F capabilities

One of the most important capabilities of the Swing toolkit is its pluggable look and feel (PL&F): a feature that it lets developers choose the appearance and behavior (or the look and feel) of the windowing components which they use in their programs.

With Swing´s PL&F capabilities, you can make the windowing components in your applications take on whatever appearance and behavior you like. If you like, you can make them look and feel just like native components of the user´s computer system. If you prefer, you can give your components a uniform cross-platform look and feel -- that is, a look and feel that always has the same appearance and behavior, no matter what kind of system is being used. A third alternative is to create an individual, custom-designed look and feel (L&F) that is unique to your application.

You can use Swing's pluggable look-and-feel capabilities in many ways. If you design corporate applications, you can use Swing's PL&F feature to work your company´s logo or other kinds of corporate graphics into GUI components. Developers of computer games can develop sets of Swing components that reflect the themes of individual games or game levels. And no matter what kind of applications you develop, you may just decide one day that although you like the appearance and behavior of an existing look and feel -- such as the Java L&F that ships with Swing -- you would like to make a few modifications, such as changing the colors of buttons or redesigning window borders. You can do all of those things, and more, by taking advantage of the PL&F capabilities built into the Swing component set.

Typically, if the developer of a program expects the program to be executed on just one platform -- such as Windows, UNIX, or the Macintosh -- the application specifies the L&F to use, and the program locks down that L&F as soon as it is loaded. If the program is designed to run on more than one platform, the programmer typically uses a cross-platform look and feel, such as the Java L&F that comes with Swing. If the programmer specifies a look and feel that isn't available on the user's computer system, Swing's default behavior is to use the Java look and feel.

Horizontal rule 

Accessibility and Swing

Another important feature of the Swing set is its accessibility -- that is, its compatibility with hardware and software designed for people with special needs, such as limited sight or the inability to operate a mouse. Accessibility is an important feature of Swing, because the Swing set is the only JavaTM-language component set that can be used to write Web content that is accessible to disabled people.

Furthermore, accessible applications can be beneficial to people who are not disabled. For instance, the Java Accessibility API makes it easy to create touch-screen programs such as those used in kiosks.

The topic of Swing accessibility is covered in much more detail in a Special Report in this issue of The Swing Connection.

Horizontal rule 

A Brief History of Swing

As noted earlier, it's possible to design a program with a set of Swing components that look and feel just like the native components used by whatever platform the program is running on. If you wrote that kind of program and a user executed it under Windows, it would look and behave like a program written specifically for Windows. If a user ran the same program on a UNIX workstation, it would just like any program written for UNIX. If someone executed it on an Apple Macintosh, it would look and behave just like any program written specifically for the Mac -- and so on.

Components that have the look and feel of the operating system they´re running are nothing new. In the pre-Swing era, AWT components were usually designed to have the look and feel of the user´s computer system. But the Swing component set improves the L&F capabilities of the AWT tool set in two important ways:

Swing components are lightweight: First, Swing components are designed in accordance with a lightweight component-design specification that was introduced with JDK 1.1. That means that Swing components don't use any platform-specific implementations (which AWT programmers often refer to as "peers.") Instead, Swing creates its components using pluggable look-and-feel (PL&F) modules that are written from scratch and don't use any peer code at all. Consequently, Swing components can typically be incorporated into a program using less code that older "heavyweight" components required. Because Swing components use fewer system resources and produce smaller and more efficient applications than their heavyweight AWT counterparts, Swing's lightweight components invariably perform better and should be used from now on in all applications.

Swing components have PL&F capabilities: The second advantage that Swing components have over AWT components is that they can be used to develop customized looks and feels. A Swing program can use either an all-purpose cross-platform L&F that always looks the same, no matter what kind of computer it's running on, or an L&F installation that always matches the particular platform on which the program is being executed. Swing also allows developers to create their own customized Swing components -- or even complete sets of custom Swing components -- that can have any kind of appearance and behavior that the developer can dream up.

Horizontal rule 

JLF and other looks and feels

In Swing, a look and feel that looks and feels the same across platforms is known, logically enough, as a customized look and feel. One cross-platform L&F -- called the Java look and feel -- ships with Swing as part of the Swing toolkit package. Swing also comes with several platform-specific L&F modules that can be run only on the platforms they are designed for. The L&F implementations currently shipped with Swing are:

  • The Java look and feel.
     
  • A Motif L&F.
     
  • A Win32 L&F for Windows 95, Windows 98, and Windows NT.
     

A Multiplex L&F that provides an easy way to extend the capabilities of a Swing user interface without having to create a new look and feel.

A Mac L&F for Macintosh systems is also available, as a separate download from the Java Developer Connection Web site.

One of the most important capacities of Swing's PL&F mechanism is that Swing figures out all by itself -- without any intervention on the part of the developer -- what look and feel is native to the computer system on which a Swing program is being executed. Typically, the developer sets a preferred L&F at startup or initialization time, and doesn't have to bother about it from then on. There is no need to use different APIs because once the developer chooses which L&F a program should use, all other PL&F operations happen automatically.

Horizontal rule 

Swing and AWT

From an API perspective, the Swing component set extends -- but does not replace -- the pre-Swing Abstract Windowing Toolkit (AWT). In fact, Swing sits atop part (but not all) of the AWT tool set, as shown in this diagram:

Swing Stack (diagram) 

The diagram shows how Swing sits on top of a number of the APIs that implement the various parts of JFC -- including the Java 2D and Drag and Drop APIs. Although both those APIs are part of JFC, they are not part of Swing. That's because certain tasks that they perform require use of native code. And Swing components, as we have seen, never rely on peer code. So Java 2D and Drag-and-Drop appear beneath, not inside, the rectangle labeled "Swing" in the diagram. So does the old-style AWT component set, which is extended (but not replaced) by Swing.

Part 2: Swing Component Architecture Right Arrow

[an error occurred while processing this directive]