Swing Application Framework (JSR-296): new concepts, new features

7 August, 2008 at 09:20 7 comments

Swing logoSwing is a widget toolkit for Java. It is part of Sun Microsystems’ Java Foundation Classes (JFC) — an API for providing a graphical user interface (GUI) for Java programs.

Swing was developed to provide a more sophisticated set of GUI components than the earlier Abstract Window Toolkit. Swing provides a native look and feel that emulates the look and feel of several platforms, and also supports a pluggable look and feel that allows applications to have a look and feel unrelated to the underlying platform.

Framework Architecture

Two classes help you manage your application: ApplicationContext and Application. The Application and ApplicationContext objects have a one-to-one relationship.

The ApplicationContext provides services to the application as shown in Figure 1.

Application Context

Figure 1. An ApplicationContext provides services to your Application instance.

Those services include the following:

  • Localizable resource management
  • Task services and monitoring
  • Event-action management
  • Session-state storage

All Swing Application Framework applications must subclass either the Application class or its SingleFrameApplication subclass. The Application class provides lifecycle methods for launching the application, starting the user interface (UI), and shutting down.

The SingleFrameApplication adds a default main GUI frame, retrieves and injects default resources, and uses the ApplicationContext to save and restore simple session state. Session state includes UI component location, size, and configuration.

Both superclasses provide an ApplicationContext, but the SingleFrameApplication class provides additional default behaviors that use the context. You probably will not use Application as your superclass. Most likely, the SingleFrameApplication class provides the default behavior you need. In the future, other subclasses should be available to handle multiframe applications as well. Regardless of the superclass, your application will use its ApplicationContext instance to access most services that the framework provides.

Application Life Cycle

All applications have a life cycle of events that are called in a specific order. Your primary Application or SingleFrameApplication subclass should start in its static main method and should launch the application from that point. The supported life cycle includes the following methods, called in this order:

  1. launch — You must call this framework method.
  2. initialize — The framework will invoke this optional overridden method.
  3. startup — The framework will invoke this overridden method.
  4. ready — The framework will invoke this optional overridden method.
  5. exit — You must call this framework method.
  6. shutdown — The framework will invoke this optional overridden method.

Your application’s minimum requirement is to call the launch method and to override the startup method. By calling the launch method, your application begins the life cycle. The launch method will then call the initialize, startup, and ready methods. You should also handle your application frame’s closing by calling the exit method, which will eventually call the shutdown method. A few examples should help make the sequence more clear.

Swing Application Framework update (Alexander Potochkin’s Blog)

As you probably know, a few weeks ago I became the new specification lead for the Swing Application Framework project (JSR-296). This project has been fairly silent for the last little while, so it is high time to continue with working on this framework and complete it timely. The main goal of this project is quite ambitious: create a Swing framework which allows to quickly create a simple Swing application and which is also flexible and extensible for the big commercial applications. The key to success it to recognize the common patterns and the best practices that help to create a good Swing application and implement them in the Swing Application Framework.

In this blog entry I want to describe the recent changes and give the reasons why it was done this way and bring up some issues to think over. Don’t hesitate to share your opinion, we make it for you and your feedback is very important for us. The project is now in transition state and the API may be changed. The latest source files you can find in the Subversion reporsitory, zipped files are also available, the previous stable version can be found here.

One frame application

The framework is to be friendly to beginners and creating an application with a single frame should be very easy. Remember the time when you were writing your first Swing application – JFrame with a main component, toolBar, statusBar and menu what questions did you have?

Here is my list:

  • How to create a JFrame?
  • How to place components properly to the frame?
  • How to show the frame?

Let me show you how it can done with the latest SAF:

public class SingleFrameExample2 extends SingleFrameApplication {
    @Override
    protected Component createMainComponent() {
        JLabel label = new JLabel("JLabel");
        label.setFont(new Font("LucidaSans", Font.PLAIN, 32));
        return label;
    }

    @Override
    protected JMenuBar createJMenuBar() {
        JMenuBar bar = new JMenuBar();
        JMenu menu = new JMenu("JMenu");
        menu.add(new JMenuItem("JMenuItem"));
        menu.add(new JMenuItem("JMenuItem"));
        bar.add(menu);
        return bar;
    }

    @Override
    protected JToolBar createJToolBar() {
        JToolBar toolBar = new JToolBar();
        toolBar.add(new JButton("JButton"));
        return toolBar;
    }

    public static void main(String[] args) {
        launch(SingleFrameExample2.class, args);
    }
}

By running this small example you’ll see the frame with the menu, toolbar and the label as the main component. This frame will have the size sufficient to all its subcomponent and be shown at the center of the screen.

Open issues

With the provided test case you can easily create one frame, but what about more realistic multi frame applications? The SingleFrameApplication creates the main frame and exits when the frame is closed. It may be not the case if there are more frames in your application, for example you may want your application to stay open until it has at least one frame visible. It is also important for the modern application to save the state of a frame (like its size and position) when it is closed and restore it when the application is started again.

All that mean that we can’t just create a frame or dialog and show it. We need a way to let the application set it up and add closing listeners or set the frame’s bounds. The current SingleFrameApplication has the

public void configureWindow(Window root, Object key)

which should be called after you create any secondary window in your application. The first parameter is the window itself and the second one is the identifier of this window, which is used to save the window’s state.

This design is not very flexible, for example it is difficult to set which window should close the application

Proposed API

Introduce a new method in the Application class

public void registerWindow(Window w, WindowContext c)

and add a new WindowContext class which will keep the identifier, know how to configure this window and whether window’s listener should close the application when this window is closed

abstract public class WindowContext {

    abstract public Object getIdentifier();

    public void configure(Window w){
    }

    //Is it a good name for this method?
    public boolean isClosingApplication() {
        return false;
    }
}

(more)

Swing Intro

[wikipedia.org]
[http://weblogs.java.net/blog/alexfromsun]
[http://java.sun.com/developer]

Bookmark and Share

Advertisements

Entry filed under: Framework, Java, Swing, Swing Application Framework. Tags: , , , .

Zend Framework – ZF: Concepts, Methods and Examples Central Authentication Service – CAS: concepts and examples

7 Comments Add your own

  • 1. Mikael Grev  |  9 August, 2008 at 09:04

    Hello Alex,

    I am personally not that fond of the WindowContext construct. They are good if you intend, and actually will, extend them in the future. The reason I don’t like them is that the inner classes is an ugly construct in Java requires much more code than simple methods on the Application class.

    Since an app should not have more than one window to close the app why not simply have:
    Application.setMainWindow(Window)?

    With your construct you still fail to cater for the fact that in a multi-frame setup you typically have many windows and when you close the last one of a certain kind, you exit.

    The solution requires further thinking IMO. I think you fall into the common Swing mistake of making everything very configurable but then fail to extend that because it can break code (you can never extend the WindowContext class without possibility to break existing code) and you end up with a complex construct that is still not very flexible in its current form.

    You can for instance exchange your current WindowContext with two simple methods in the Application class and that is much much simpler for a Swing newbie to find and use.

    Cheers,
    Mikael

    Reply
  • 2. blogspot.com  |  22 April, 2013 at 06:58

    Excellent article. Keep writing such kind of information on your page.
    Im really impressed by it.
    Hey there, You’ve performed an incredible job. I will certainly digg it and for my part recommend to my friends. I’m sure they’ll be benefited from this website.

    Reply
  • 3. Matthias  |  30 April, 2013 at 23:41

    That is a good tip particularly to those fresh to the blogosphere.
    Simple but very precise information… Thanks for sharing this one.
    A must read article!

    Reply
  • 4. similar web page  |  5 May, 2013 at 17:45

    Hey there superb blog! Does running a blog similar to this take a lot of work?
    I have absolutely no understanding of coding however I was hoping to
    start my own blog in the near future. Anyways, if
    you have any recommendations or techniques for new blog owners please share.
    I understand this is off subject but I just had to ask.
    Thank you!

    Reply
  • 5. Rod  |  7 May, 2013 at 02:53

    I quite like looking through a post that can make people think.
    Also, thank you for allowing for me to comment!

    Reply
  • 6. Pasquale  |  4 July, 2014 at 06:01

    Great post. I was checking constantly this blog
    and I am impressed! Extremely helpful info
    particularly the last part 🙂 I care for such info a lot.

    I was looking for this certain information for a long time.
    Thank you and best of luck.

    Reply
  • 7. toko tas online  |  16 July, 2014 at 00:37

    I know this if off topic but I’m looking into starting my own blog
    and was wondering what all is required to get setup?
    I’m assuming having a blog like yours would cost a pretty
    penny? I’m not very web smart so I’m not 100% positive.
    Any tips or tips would be greatly appreciated. Kudos

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


IT Passion’s Store

Archives

Communities

Get the Source
OSGi supporter
JUG Milano

Upcoming Events



....

Blog Stats

  • 343,227 hits

My PageRank

What's My Google PageRank?

%d bloggers like this: