Wrap-up
This tutorial dealt with some intermediate-level issues that you may encounter when working with Swing when developing UI applications. It is meant to build upon the knowledge you gained in the Introduction to Swing tutorial, or to build on your already existing Swing knowledge. It dealt with some common issues that UI developers come across in their applications. I've attempted to pick out a combination of the most interesting and most important issues.
As I've discussed many times throughout both tutorials, these tutorials are by no means exhaustive in their coverage of Swing -- there's just far too much to learn to be summarized in two tutorials. Hopefully what you've learned in this tutorial will not only improve your knowledge in a few fields of Swing, but pique your interest in Swing as a whole and lead you to delve more into some areas of it.
In this tutorial, through only a few quick lessons, we've managed to turn our example application, the flight reservation system, from a basic application that merely provided functionality to a more professional application that solved all the issues that Swing applications need to deal with. We started with an application that looked like this:
And turned it into this:
For completeness, let's review the important lessons learned from this tutorial:
-
Intermediate-level JTable: In addition to learning how many of a table's properties can be changed to quickly change how a table looks, you also learned the following things about the JTable:
-
TableRenderers: You learned that you aren't constrained by the JTable's built-in color scheme for its cells. By creating your ownTableRenderer, you can control how each cells appears in the table, when it is selected or deselected, and even how to provide feedback about the application (as when we grayed out unavailable flights). -
TableModel: In addition to controlling the data, theTableModelcan also control how data is aligned in the column, and whether a cell can be edited. -
TableEditors: The JTable has many built-in editors for common classes that appear in data (Strings,Integers), but creating a custom editor for other data is difficult and beyond the scope of this tutorial. -
TableSorter: Users have come to expect that they'll be able to sort their table's data in a meaningful way by clicking on the column headers. The JTable that is shipped with Swing does not support sorting at all, but Sun provides aTableSorterclass (that isn't shipped with the JDK) that takes care of sorting for you.
-
-
Thread safety: Thread safety is an important issue in Swing, as poor thread management can lead to a user getting locked out of the application or an ugly gray rectangle appearing instead of the application UI.
-
SwingUtilities.invokeLater()should be used whenever a thread besides the event-dispatch thread needs to perform UI work. - The
SwingWorkerclass (another class not shipped in the JDK but published by Sun) should be used on the event-dispatch thread whenever a time-consuming action (like querying a database) must be performed.
-
-
Custom components: While Swing provides nearly every component you could want, sometimes these components don't exactly work the way you want them too, or sometimes even the many components of Swing aren't enough to provide the widget your application needs. Modifying an existing component allows you to change the way a Swing widget works in your application. The best way to do this is to subclass the existing Swing widget and override the
paintComponent()method to tailor the component to your needs. Creating a new custom component allows you to create original one-of-a-kind components not contained in Swing. The steps to follow to create a new component are to identify the components you'll need, lay them out properly, get them working with each other to get the new component working properly, and finally to wrap a public API around the new component to make it easy to work with. -
Custom look and feel: Creating a custom look and feel is a very difficult, complex, and time-consuming process, and probably deserves a tutorial all its own. However, there are easy ways to quickly change some behavior in your application that you learned in this tutorial:
- Use the UIManager to override the properties used in an existing LookAndFeel, and supply your own colors, fonts, and borders to tailor the existing look and feel to your needs.
- Use the new Synth LookAndFeel, an addition to Swing that allows you to create custom look and feels far easier and quicker than before.


