The Source for Java Technology Collaboration
User: Password:



John O'Conner

John O'Conner's Blog

Swing Application Framework: Lifecycle Events

Posted by joconner on June 12, 2007 at 12:29 AM | Comments (2)

confirmexit.gif

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;
    
    @Override
    protected void startup() {
        getMainFrame().setTitle("BasicSingleFrameApp");
        label = new JLabel("Hello, world!");
        label.setFont(new Font("SansSerif", Font.PLAIN, 22));
        show(label);
    }
    
    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;
    
    @Override
    protected void startup() {
        getMainFrame().setTitle("ConfirmExit");
        exitButton = new JButton("Exit Application");
        exitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                exit(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?") ==
                                JOptionPane.YES_OPTION;
                return bOkToExit;
            }
            public void willExit(EventObject event) {
                
            }
        });
        show(exitButton);
    }

    @Override
    protected void shutdown() {
        // the default shutdown saves session window state
        super.shutdown();
        // 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.


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • It seems window listeners that check for application shutdown can't deal with the shutdown sequence in windows. That is when you have a swing application that has some persistence logic in the window listener and you press the hardware shutdown button in you computer case, swing doesn't call the window listener before java(and the computer) shuts down. Is this still the case with the Application Framework shutdown hooks?

    Posted by: i30817 on June 12, 2007 at 02:19 AM

  • Autogenerated desktop application hide but not exit on close.
    Code to DO_NOTHING_ON_CLOSE:
    @Override protected void startup() {
    ExampleView v = new ExampleView(this);
    v.getFrame().setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    show(v);
    }

    Please look at: application\SingleFrameApplication.java line 197, c != mainFrame:
    JFrame mainFrame = getMainFrame();
    if (c == mainFrame) {
    mainFrame.addWindowListener(new MainFrameListener());
    mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    }
    else if (root instanceof Window) { // close == save session state
    Window window = (Window)root;
    window.addHierarchyListener(new SecondaryWindowListener());
    }

    Posted by: olejnikmariusz on August 23, 2007 at 07:15 PM



Only logged in users may post comments. Login Here.


Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds