Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Intermediate Swing

Michael Abernethy
Michael Abernethy is an IBM employee who is currently the test team lead for the WebSphere System Management team, based in Austin, Texas. Prior to this assignment, he was a UI developer working in Swing at multiple customer locations.

Summary:  This tutorial builds on Introduction to Swing, which introduced the basics of Swing programming and the flight reservation system application. In this hands-on tutorial, Swing programmer Michael Abernethy walks you through more advanced Swing techniques like writing thread-safe code, building custom components, and customizing the look and feel to create a more polished and powerful UI.

Date:  29 Jun 2005
Level:  Introductory

Activity:  32119 views
Comments:  

Wrap-up

Summary

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:

flight reservation system at beginning

And turned it into this:

flight reservation system at end

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 own TableRenderer, 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, the TableModel can 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 a TableSorter class (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 SwingWorker class (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.

7 of 9 | Previous | Next

Comments



Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Java technology
ArticleID=131939
TutorialTitle=Intermediate Swing
publish-date=06292005
author1-email=mabernet@us.ibm.com
author1-email-cc=jaloi@us.ibm.com

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).