Oracle Blog

Sessions, Hands-On Labs, and more

Swing Application Framework and Beans Binding

Okay, I admit it. I was the co-author of “Java Swing,” a book which was published by O'Reilly ten years ago. I also admit that I've had several ideas throughout the years as to what would make a decent framework for Swing applications, including support for cataloging actions and resources, and cacheing of session state (window size, position, etc.). So, it was with great relief that a few years ago, I found that Hans Meuller on the Swing team was taking on this task in the new Swing Application Framework (JSR 296). I was equally glad to see that the scope of this project was something that “could be learned in a day.”

If you've ever worked with threads in Swing, or even the SwingWorker class, you probably know that it can be a bit of a challenge. Fortunately, JSR 296 will allow you to create tasks that can be started and monitored with the use of a TaskMonitor. In addition, there is a TaskService class that can help you organize each of your tasks and fire them off.

The Swing Application Framework also supports various lifecycle methods that you get when extending specific framework classes, such as initialize(), startup(), ready(), and shutdown(). Just extend the appropriate class, override these methods, and you won't have to worry about trapping any obscure events and reacting to them again.

I was also very impressed by the resource injection. Basically, this means that you can do something like:

Resource file:

btnShowTime.text = Show current time!
btnShowTime.icon = refresh.png

Java class:

btnShowTime = new JButton();
btnShowTime.setName("btnShowTime");

And at this point, the resources are injected into the btnShowTime class automatically. In addition, you can even do something like this:

There is also a new API out there for beans binding. If you've never heard of beans binding before, the idea is that two properties in two different beans need to remain synchronized. This is actually part of JSR 295. In short, this means you can do something like this:

This will create a binding between the two objects such that if a value on either side changes, the other value will instantaneously be changed as well. It's simple, elegant, and it works! Kudos to Shannon Hickey and Scott Violet for a great addition to the Java APIs.

You are right. Both (JSR-295 and JSR-296) specs are really good. You can even build UIs visually already (both are already bundled with Netbeans 6.0). Then you can specify the binding expressions already in the visual editor.