Skip to main content

Swing Application Framework: Lifecycle Events

Posted by joconner on June 12, 2007 at 12:29 AM PDT


All applications have a lifecycle 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 lifecycle includes the following methods, called in the same 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 Application.launch method and to override the startup method. By calling the launch method, your application begins the lifecycle. 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.

The following code example shows how to subclass the SwingleFrameApplication class and override the startup method.

public class BasicSingleFrameApp extends SingleFrameApplication {
    JLabel label;
    protected void startup() {
        label = new JLabel("Hello, world!");
        label.setFont(new Font("SansSerif", Font.PLAIN, 22));
    public static void main(String[] args) {
        Application.launch(BasicSingleFrameApp.class, args);


Optionally, override the initialize method to check db connections, process command line arguments, etc. The default method in the superclass does nothing.

If you need to do something after your UI is visible, override the ready method.

Finally, the SingleFrameApplication class implements a WindowsAdapter that processes the window-closing event by saving session state, contacting ExitListener objects, and finally calling System.exit(0). You probably don't need to override that behavior, but you might want to register your own ExitListener to give your application an opportunity to veto the exit request or query the user for exit confirmation. The following example shows how you might implement an ExitListener:

public class ConfirmExit extends SingleFrameApplication {
    private JButton exitButton;
    protected void startup() {
        exitButton = new JButton("Exit Application");
        exitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
        addExitListener(new ExitListener() {
            public boolean canExit(EventObject e) {
                boolean bOkToExit = false;
                Component source = (Component) e.getSource();
                bOkToExit = JOptionPane.showConfirmDialog(source,
                                "Do you really want to exit?") ==
                return bOkToExit;
            public void willExit(EventObject event) {

    protected void shutdown() {
        // the default shutdown saves session window state
        // now perform any other shutdown tasks you need
        // ...
     * @param args the command line arguments
    public static void main(String[] args) {
        Application.launch(ConfirmExit.class, args);

There's a lot more to lifecycle event methods. Check out the reference implementation of the Swing Application Framework and its demos to learn more.

Related Topics >>