HerbSchildt.com

 

Home
Up
About Herb Schildt
Beginner's Corner
Current Books
What's New?
Where to Buy
Book Code
Speaking/Training
Errata
Contact Info

New Releases!

Herb's  Programming Cookbooks

C++

Java

 

 

 

Swing: Because First Impressions Matter

By

Herbert Schildt

 

"Like many things in life, first impressions matter, and programming is no exception. The way that a program looks on the screen and the feel of its controls are the basis on which the user initially judges your work. Because first impressions often become lasting impressions, it is important that a program’s user interface be both visually pleasing and easy to use. Fortunately, both goals can be achieved by using Swing."

As I write this, my latest book Swing: A Beginner's Guide has just been released, and the preceding paragraph is from its preface. I use it here because it makes an important point: The quality and reliability of the graphical user interface (GUI) often determines the success or failure of a project. As a result, the creation of appealing, consistent, and efficient user interfaces has become an important part of programming. For Java programmers, the way to create such interfaces is through the use of Swing.

Swing is Java's lightweight GUI toolkit. It provides a rich set of visual components, such as push buttons, check boxes, lists, and trees, that form the basis of the modern GUI environment. These components can be used as-is, or customized to fit virtually any need. Swing is used to create the high-performance, professional-quality user interfaces demanded by today's Java applications.

Swing has become so integral to Java programming that no one can truly be a master Java programmer without also mastering Swing. If you are just beginning to learn Java, then Swing is an area in which you will want to achieve proficiency. If you are an experienced pro, you will want to take a second look at this important subsystem. In Java, things change very quickly, and there have been several updates and enhancements to Swing over the past few years. Simply put: Swing is an area in which no programmer can afford to fall behind.

It is interesting to note that Swing did not exist in the early days of Java. Rather, it was a response to certain deficiencies present in Java's original GUI subsystem: the Abstract Window Toolkit (AWT). The AWT defines a basic set of components that support a usable, but limited graphical interface. One limitation is caused by the AWT's reliance on platform-specific equivalents, or peers, to represent the visual components. Because the AWT components translate to native code resources (rather than Java code), they are referred to as heavyweight. The use of heavyweight components led to several problems. For example, they are always rectangular and opaque.

Not long after Java's original release, it became apparent that the limitations and restrictions present in the AWT were sufficiently serious to require a better approach. The solution was Swing. Introduced in 1997, Swing was included as part of the Java Foundation Classes (JFC). Swing was initially available for use with Java 1.1 as a separate library. However, beginning with Java 1.2, Swing (and the rest of JFC) was fully integrated into the Java.

Swing addresses the limitations associated with the AWT's components through the use of two key features: lightweight components and a pluggable look and feel. Although they are largely transparent to the programmer, these two features are at the foundation of Swing's design philosophy and the reason for much of its power and flexibility. Let's look at each.

With very few exceptions, Swing components are lightweight. This means that a component is written entirely in Java. They do not rely on platform-specific peers. Lightweight components have some important advantages, including efficiency and flexibility. For example, a lightweight component can be transparent, which enables non-rectangular shapes. Furthermore, the look and feel of each component is fully under the control of Swing. This means that each component will work in a consistent manner across all platforms. It also means that a component can be easily customized to precisely fit the needs of the application.

Because each Swing component is rendered by Java code rather than by platform-specific peers, it is possible to separate the look and feel of a component from the logic of the component, and this is what Swing does. Separating out the look and feel provides a significant advantage: it becomes possible to change the way that a component is rendered without affecting any of its other aspects. In other words, it is possible to "plug in" a new look and feel for any given component without creating any side effects in the code that uses that component.

The pluggable look and feel is made possible by Swing's use of a modified version of the classic model-view-controller (MVC) architecture. In MVC terminology, the model corresponds to the state information associated with the component. For example, in the case of a check box, the model contains a field that indicates if the box is checked or unchecked. The view determines how the component is displayed on the screen, including any aspects of the view that are affected by the current state of the model. The controller determines how the component reacts to the user. For example, when the user clicks a check box, the controller reacts by changing the model to reflect the user’s choice (checked or unchecked). This then results in the view being updated. By separating a component into a model, a view, and a controller, the specific implementation of each can be changed without affecting the other two. For instance, different view implementations can render the same component in different ways without affecting the model or the controller.

Although the MVC architecture and the principles behind it are conceptually sound, the high level of separation between the view and the controller was not beneficial for Swing components. Instead, Swing uses a modified version of MVC that combines the view and the controller into a single logical entity called the UI delegate. For this reason, Swing’s approach is called either the model-delegate architecture or the separable model architecture. Therefore, although Swing’s component architecture is based on MVC, it does not use a classical implementation of it.

Swing is a very large system and (at the time of this writing) it is organized into 17 packages. The main package is javax.swing and it contains (among other items) all of Swing's component classes. They are shown here.

JApplet

JButton

JCheckBox

JCheckBoxMenuItem

JColorChooser

JComboBox

JComponent

JDesktopPane

JDialog

JEditorPane

JFileChooser

JFormattedTextField

JFrame

JInternalFrame

JLabel

JLayeredPane

JList

JMenu

JMenuBar

JMenuItem

JOptionPane

JPanel

JPasswordField

JPopupMenu

JProgressBar

JRadioButton

JRadioButtonMenuItem

JRootPane

JScrollBar

JScrollPane

JSeparator

JSlider

JSpinner

JSplitPane

JTabbedPane

JTable

JTextArea

JTextField

JTextPane

JTogglebutton

JToolBar

JToolTip

JTree

JViewport

JWindow

     

Notice that all component classes begin with the letter J. For example, the class for a label is JLabel, the class for a push button is JButton, and the class for a check box is JCheckBox. In addition to these component classes, Swing provides many interfaces that define component models and several classes that determine a component's look and feel. It also supplies all manner of "helper" classes and interfaces. As a result, Swing contains hundreds of separate, but related, classes and interfaces.

Because of its size, some programmers initially find Swing quite intimidating. This is readily apparent on the Java forums of various programmer-related websites. Even a cursory examination of these forums will turn up numerous questions about Swing. Furthermore, in all the years that I have been writing about Java, more readers have asked me to write a book about Swing than about any other Java topic. Clearly, Swing can present a daunting challenge to even the most talented newcomer. Despite the challenges, Swing is well worth the time and effort that it takes to master. It is a critical part of successful Java programming.

Remember, you only get one chance to make a first impression. Mastery of Swing helps ensure that your program's first impression will be a good one!

 

 

  © 2008  HerbSchildt.com  All rights reserved worldwide. No duplication allowed without prior written permission.