|
|
||
John O'Conner's BlogSwing Application Framework: Lifecycle EventsPosted by joconner on June 12, 2007 at 12:29 AM | Comments (2)
All applications have a lifecycle of events that are called in a specific order. Your primary
Your application's minimum requirement is to call the The following code example shows how to subclass the
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 If you need to do something after your UI is visible, override the Finally, the
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: CommentsComments are listed in date ascending order (oldest first) | Post Comment
| ||
|
|