http://www.sun.com/ http://java.sun.com/ http://www.sun.com/javaone
JavaOne - Experiencing Java technology through education, industry, and community
2006 Conference
Topics
Sessions
   General Sessions
Hands-on Labs
Schedule
Schedule Builder
Register
Pavilion
   Cosponsors
   Exhibitors
   Media
   Presentation Theater
Java University
Daily Activities
Event Connect
Alumni
   Alumni FAQ
Multimedia Sessions
Community
JavaOne Online
Forums
java.sun.com
java.net
java.com
sun.com/developers
Java Wear & Books
Home > General Sessions

Eight Ways to Be More Productive Developing Swing Applications

by Dana Nourie

The room was full for this session on speeding up Java Foundation Classes/Swing (JFC/Swing) production. The speaker, Ben Galbraith, asked the audience to raise their hands to show who had experience with Swing, who was just learning, and who considered Swing a productive technology to create user interfaces (UIs). The room was evenly divided between experienced and new users, and only a few hands went up in response from those who considered Swing a productive environment in which to create UIs.

Galbraith agreed that Swing technology could be difficult, but he demonstrated how to simplify this technology and make it more productive.

Use a Cross-Platform Look and Feel

Developers know that creating widgets for a specific platform can reduce the number of users for their applications. And there is the problem of varying monitor sizes and different resolutions. But Swing's plaf widgets for look and feel can be unappealing, and creating custom widgets can be even trickier, time-consuming, and difficult to use effectively. Vendors are unable to support a small set of looks, let alone a larger base. According to Galbraith, the solution lies in creating a cross-platform look and feel using the JGoodies Plastic family, which has the following benefits:

  • Improved readability, legibility, and usability.
  • Improved aesthetics, which look good on most desktops
  • Simplified multiplatform support
  • Precise microdesign

See http://www.jgoodies.com/products for details.

Use a GUI Builder

In the recent past, graphical user interface (GUI) builders were difficult to use and did not do everything that developers needed them to. That has changed dramatically, especially in the last two years. Now, you can get great GUI builders that save a lot of time and frustration. What you can do in a few minutes in the builder would likely take you much longer to code by hand.

Galbraith recommends the following IDEs:

Secondly, Galbraith recommends decoupling your applications from your GUI builder. Load the UI definition at runtime and bind behaviors to them. Dynamic and static GUI building can be mixed easily, and you can tweak a visually built GUI. This is often not the case with handwritten code, where inconsistencies and typos can have you searching line after line of code for errors.

Avoid Swing's Default Layout Managers

Learning all of Swing's default layout managers can take a while, and getting all your objects where you want them and the size you need them in the managers can be difficult. According to Galbraith, everything you need is in two modern layout managers:

FormLayout is a powerful, flexible and precise general-purpose layout manager. It places components in a grid of columns and rows, allowing specified components to span multiple columns or rows.

GroupLayout will also be included in the final release of Java Platform, Standard Edition (Java SE) 6.

Externalize Widget Styling

Manually styling widgets leads to inconsistencies and can be impossible to get right. For instance, setting a font in bold in most GUI builders results in hard-coding the font family or type. Instead, consider using Cascading Style Sheets (CSS) to style Swing components. CSS can provide simple styles for your Swing fonts in much the same way as they do for the web.

For example, this is a sample of CSS:

foo { font-family: Arial,sans-serif; border: 1px solid black }
.bar { margin: 4pt }

Then use client properties to assign selectors, like this:

org.galbraiths.clarity.styleClass
org.galbraiths.clarity.styleId

Or you can use Swing's name property. In this way, you use an external file:

JTextField.myStyleClass {
font-size: -2pt;
font-weight: bold;
font-family: Courier New;Courier;
}

Through your code, you indicate this:

JComponent.putClientProperty("style", "font-size: -2pt; _");

Then you apply the styles at runtime:

JFrame frame = new JFrame("My Frame");
RuntimeForm form = RuntimeFormFactory.getRuntimeForm("Foo");
frame.getContentPane().add(form.getRootComponent());
FormDecorator.decorate(frame.getContentPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

Employ Declarative Widget Configuration

Performing common configuration on widgets can be tedious. Tables are the best example if you consider the amount of code required to center a column's contents. A declarative widget configuration system — such as DSL, XML, a properties file, or whatever else you prefer — helps dramatically.

Use Binding and Validation Frameworks

Getting and setting values on widgets and converting them to the appropriate type is tedious, and so is displaying meaningful error messages to the user.

Binding and validation frameworks perform all of this plumbing for you.

  • Use PropertyChangeListeners and Swing listeners
  • Manually invoke firePropertyChanged in all setters
  • Copy values at explicit moments, such as copyValuesFromUI() and copyValuesToUI()
  • Use listeners with widgets, but explicitly copy them from beans

Recommended key binding frameworks:

  • JGoodies bindings
  • SwingLabs bindings
  • Key validation frameworks:
  • JGoodies validation

Enhance Swing's Action

Event handling in Swing has a few weaknesses. Disabling components properly is tricky, and threading can be painful and tedious. Reusing event-handling logic across multiple event types is repetitious.

To overcome these annoyances and speed up production, Action can be subclassed and enhanced. You can also add a lot of convenience functionality to Action in the process. Create a simplified Listener API. Enhanced Actions can emulate this approach:

bindAction(action, component, Event.MouseClicked);

You can define a sensible, default event mapping for components that don't natively support actions, such as bindAction on a JTable binds to selection changing.

Introduce a Form Concept

The act of creating and displaying a screen and handling navigation is time-consuming. Standardizing how these elements are resolved increases development speeds and productivity.

Unfortunately, we ran short of time for this session, but Galbraith demonstrated that you can achieve tremendous productivity with Swing by focusing on a single look and feel, using a GUI builder and new layout managers, reducing API complexity — and the amount of code you need to write — by externalizing styling and configuration, automating binding and validation, and standardizing forms.


 Back to top


Rate and Review
Tell us what you think of the content of this page.
Excellent   Good   Fair   Poor  
Comments:
If you would like a reply to your comment, please submit your email address:
Note: We may not respond to all submitted comments.