Sun Java Solaris Communities My SDN Account Join SDN
 
Article

Java Developer Connection: The Swing Tutorial Updates

 
 

Articles Index


A new version of the Java Tutorial trail Creating a GUI with JFC/Swing, also known as The Swing Tutorial, was recently released. The authors are updating this trail from version 1.1/1.2 to version 1.4 of the Java 2 Platform, Standard Edition (J2SE). This article highlights some of the changes you'll find now and in the near future.

The tutorial authors will be pushing out updates in stages. Some pages of The Swing Tutorial are preliminary and you may see comments noting where the authors intend to make changes. As they are doing this, they would like your comments and suggestions. At the end of this article, there is a link to the form that you can use to give them your feedback.

New Pages of The Swing Tutorial

Pages added in the most recent version of the tutorial include:

The 1.4 examples index pages list the many examples that have been updated to the 1.4 API, and include links to JNLP files that let you run the examples using Java Web Start. The examples indexes also have links to where each example is discussed and to the files required for each example, including the full source code.

Often updating the code to 1.4 required few or no changes. Still, the authors took advantage of the opportunity to improve comments, make the examples more robust, and use new 1.4 features such as look-and-feel-provided decorations on windows.

The biggest changes, so far, were to the TextFieldDemo example. The authors changed the example to use the JFormattedTextField class (introduced in 1.4) instead of JTextField. This allowed them to eliminate three custom classes, almost halving the amount of source code in the demo's implementation. Here's a picture of resulting demo, now called FormattedTextFieldDemo:

A snapshot of the FormattedTextFieldDemo, which shows a mortgage calculator.

Click here to run the demo using Java Web Start. If you have trouble running the demo, please visit the tutorial's new Java Web Start page for troubleshooting tips. You can find the source code for FormattedTextFieldDemo in the 1.4 component examples index.

The recently added How to Use Key Bindings section provides detailed information on the appropriate use of key bindings. As the tutorial explains, key bindings are often used behind the scenes by mnemonics (supported by all buttons) and accelerators (supported by menu items). When you need to make your program react to user key presses that aren't otherwise handled by the Swing API, you can use either key bindings or key listeners. For responding to individual keys, key bindings are usually more appropriate and tend to result in more easily maintained code.

Updated Pages of The Swing Tutorial

While most of the tutorial's existing pages have not been completely updated to 1.4, some have. These updates include:

The changes to How to Use Actions are probably the most interesting, so far. This page now covers more Action properties, including SHORT_DESCRIPTION, ACCELERATOR_KEY, NAME, and MNEMONIC_KEY. A new API table lists the properties, the components they're automatically applied to, and the purpose of each property. For example, if you set the SHORT_DESCRIPTION property of an action, its value is automatically used as the tool tip text for any AbstractButton descendant controlled by the action.

The existing action example, ActionDemo, has been updated to use more properties. Here's a picture of ActionDemo:

A snapshot of ActionDemo, which uses actions to coordinate menus and buttons.

Click here to run the demo using Java Web Start. If you have trouble running the demo, please visit the tutorial's new Java Web Start page for troubleshooting tips. You can find the source code for ActionDemo in the appropriate_1.4_examples_index.

How You Can Contribute

The writers plan to have the new edition of the book The JFC Swing Tutorial available by next June, in time for JavaOne. You can help this book and the The Swing Tutorial Web site be their best by offering your suggestions for using the tutorial feedback page. Also, if you're interested in learning more about Swing programming, please register for the tutorial announcement list so you can be notified when the authors update The Swing Tutorial.

Take the Quiz

Feel like you know The Swing Tutorial pretty well? Take the quiz to make sure.