Class Application
- java.lang.Object
-
- org.jdesktop.application.AbstractBean
-
- org.jdesktop.application.Application
-
- Direct Known Subclasses:
ActionExample1,ActionExample2,ActionExample3,ActionExample4,ApplicationExample1,ApplicationExample2,ExitExample1,LocalStorageExample1,SessionStorageExample1,SingleFrameApplication
@ProxyActions({"cut","copy","paste","delete"}) public abstract class Application extends AbstractBean
The base class for Swing applications.This class defines a simple lifecyle for Swing applications:
initialize,startup,ready, andshutdown. TheApplication'sstartupmethod is responsible for creating the initial GUI and making it visible, and theshutdownmethod for hiding the GUI and performing any other cleanup actions before the application exits. Theinitializemethod can be used configure system properties that must be set before the GUI is constructed and thereadymethod is for applications that want to do a little bit of extra work once the GUI is "ready" to use. Concrete subclasses must override thestartupmethod.Applications are started with the static
launchmethod. Applications use theApplicationContextsingletonto find resources, actions, local storage, and so on.All
Applicationsubclasses must overridestartupand they should callexit()(which callsshutdown) to exit. Here's an example of a complete "Hello World" Application:public class MyApplication extends Application { JFrame mainFrame = null; @Override protected void startup() { mainFrame = new JFrame("Hello World"); mainFrame.add(new JLabel("Hello World")); mainFrame.addWindowListener(new MainFrameListener()); mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); mainFrame.pack(); mainFrame.setVisible(true); } @Override protected void shutdown() { mainFrame.setVisible(false); } private class MainFrameListener extends WindowAdapter { public void windowClosing(WindowEvent e) { exit(); } } public static void main(String[] args) { Application.launch(MyApplication.class, args); } }The
mainFrame'sdefaultCloseOperationis set toDO_NOTHING_ON_CLOSEbecause we're handling attempts to close the window by callingApplicationContextexit().Simple single frame applications like the example can be defined more easily with the
SingleFrameApplicationApplicationsubclass.All of the Application's methods are called (must be called) on the EDT.
All but the most trivial applications should define a ResourceBundle in the resources subpackage with the same name as the application class (like
resources/MyApplication.properties). This ResourceBundle contains resources shared by the entire application and should begin with the following the standard Application resources:Application.name = A short name, typically just a few words Application.id = Suitable for Application specific identifiers, like file names Application.title = A title suitable for dialogs and frames Application.version = A version string that can be incorporated into messages Application.vendor = A proper name, like Sun Microsystems, Inc. Application.vendorId = suitable for Application-vendor specific identifiers, like file names. Application.homepage = A URL like http://www.javadesktop.org Application.description = One brief sentence Application.lookAndFeel = either system, default, or a LookAndFeel class name
The
Application.lookAndFeelresource is used to initialize theUIManager lookAndFeelas follows:system- the system (native) look and feeldefault- use the JVM default, typically the cross platform look and feel- a LookAndFeel class name - use the specified class
- See Also:
SingleFrameApplication,ApplicationContext,UIManager.setLookAndFeel(javax.swing.LookAndFeel)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceApplication.ExitListenerGive the Application a chance to veto an attempt to exit/quit.
-
Constructor Summary
Constructors Modifier Constructor Description protectedApplication()Not to be called directly, seelaunch.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaddExitListener(Application.ExitListener listener)Add anExitListenerto the list.protected voidend()Called byexitto terminate the application.voidexit()Gracefully shutdown the application, callsexit(null)This version of exit() is convenient if the decision to exit the application wasn't triggered by an event.voidexit(java.util.EventObject event)Gracefully shutdown the application.ApplicationContextgetContext()The ApplicationContext singleton for this Application.Application.ExitListener[]getExitListeners()All of theExitListenersadded so far.static ApplicationgetInstance()TheApplicationsingleton, or a placeholder iflaunchhasn't been called yet.static <T extends Application>
TgetInstance(java.lang.Class<T> applicationClass)TheApplicationsingleton.voidhide(View view)protected voidinitialize(java.lang.String[] args)Responsible for initializations that must occur before the GUI is constructed bystartup.static <T extends Application>
voidlaunch(java.lang.Class<T> applicationClass, java.lang.String[] args)Creates an instance of the specifiedApplicationsubclass, sets theApplicationContextapplicationproperty, and then calls the newApplication'sstartupmethod.voidquit(java.awt.event.ActionEvent e)The defaultActionfor quitting an application,quitjust exits the application by callingexit(e).protected voidready()Called after the startup() method has returned and there are no more events on thesystem event queue.voidremoveExitListener(Application.ExitListener listener)Remove anExitListenerfrom the list.voidshow(View view)protected voidshutdown()Called when the applicationexits.protected abstract voidstartup()Responsible for starting the application; for creating and showing the initial GUI.-
Methods inherited from class org.jdesktop.application.AbstractBean
addPropertyChangeListener, addPropertyChangeListener, firePropertyChange, firePropertyChange, getPropertyChangeListeners, removePropertyChangeListener, removePropertyChangeListener
-
-
-
-
Method Detail
-
launch
public static <T extends Application> void launch(java.lang.Class<T> applicationClass, java.lang.String[] args)
Creates an instance of the specifiedApplicationsubclass, sets theApplicationContextapplicationproperty, and then calls the newApplication'sstartupmethod. Thelaunchmethod is typically called from the Application'smain:public static void main(String[] args) { Application.launch(MyApplication.class, args); }TheapplicationClassconstructor andstartupmethods run on the event dispatching thread.- Parameters:
applicationClass- theApplicationclass to launchargs-mainmethod arguments- See Also:
shutdown(),ApplicationContext.getApplication()
-
initialize
protected void initialize(java.lang.String[] args)
Responsible for initializations that must occur before the GUI is constructed bystartup.This method is called by the static
launchmethod, beforestartupis called. Subclasses that want to do any initialization work beforestartupmust override it. Theinitializemethod runs on the event dispatching thread.By default initialize() does nothing.
- Parameters:
args- the main method's arguments.- See Also:
launch(java.lang.Class<T>, java.lang.String[]),startup(),shutdown()
-
startup
protected abstract void startup()
Responsible for starting the application; for creating and showing the initial GUI.This method is called by the static
launchmethod, subclasses must override it. It runs on the event dispatching thread.
-
ready
protected void ready()
Called after the startup() method has returned and there are no more events on thesystem event queue. When this method is called, the application's GUI is ready to use.It's usually important for an application to start up as quickly as possible. Applications can override this method to do some additional start up work, after the GUI is up and ready to use.
-
shutdown
protected void shutdown()
Called when the applicationexits. Subclasses may override this method to do any cleanup tasks that are neccessary before exiting. Obviously, you'll want to try and do as little as possible at this point. This method runs on the event dispatching thread.
-
exit
public final void exit()
Gracefully shutdown the application, callsexit(null)This version of exit() is convenient if the decision to exit the application wasn't triggered by an event.- See Also:
exit(EventObject)
-
exit
public void exit(java.util.EventObject event)
Gracefully shutdown the application.If none of the
ExitListener.canExit()methods return false, calls theExitListener.willExit()methods, thenshutdown(), and then exits the Application withend. Exceptions thrown while running willExit() or shutdown() are logged but otherwise ignored.If the caller is responding to an GUI event, it's helpful to pass the event along so that ExitListeners' canExit methods that want to popup a dialog know on which screen to show the dialog. For example:
class ConfirmExit implements Application.ExitListener { public boolean canExit(EventObject e) { Object source = (e != null) ? e.getSource() : null; Component owner = (source instanceof Component) ? (Component)source : null; int option = JOptionPane.showConfirmDialog(owner, "Really Exit?"); return option == JOptionPane.YES_OPTION; } public void willExit(EventObejct e) {} } myApplication.addExitListener(new ConfirmExit());TheeventObjectargument may be null, e.g. if the exit call was triggered by non-GUI code, andcanExit,willExitmethods must guard against the possibility that theeventObjectargument'ssourceis not aComponent.- Parameters:
event- the EventObject that triggered this call or null- See Also:
addExitListener(org.jdesktop.application.Application.ExitListener),removeExitListener(org.jdesktop.application.Application.ExitListener),shutdown(),end()
-
end
protected void end()
Called byexitto terminate the application. CallsRuntime.getRuntime().exit(0), which halts the JVM.- See Also:
exit()
-
addExitListener
public void addExitListener(Application.ExitListener listener)
Add anExitListenerto the list.- Parameters:
listener- theExitListener- See Also:
removeExitListener(org.jdesktop.application.Application.ExitListener),getExitListeners()
-
removeExitListener
public void removeExitListener(Application.ExitListener listener)
Remove anExitListenerfrom the list.- Parameters:
listener- theExitListener- See Also:
addExitListener(org.jdesktop.application.Application.ExitListener),getExitListeners()
-
getExitListeners
public Application.ExitListener[] getExitListeners()
All of theExitListenersadded so far.- Returns:
- all of the
ExitListenersadded so far.
-
quit
@Action public void quit(java.awt.event.ActionEvent e)
The defaultActionfor quitting an application,quitjust exits the application by callingexit(e).- Parameters:
e- the triggering event- See Also:
exit(EventObject)
-
getContext
public final ApplicationContext getContext()
The ApplicationContext singleton for this Application.- Returns:
- the Application's ApplicationContext singleton
-
getInstance
public static <T extends Application> T getInstance(java.lang.Class<T> applicationClass)
TheApplicationsingleton.Typically this method is only called after an Application has been launched however in some situations, like tests, it's useful to be able to get an
Applicationobject without actually launching. In that case, an instance of the specified class is constructed and configured as it would be by thelaunchmethod. However it'sinitializeandstartupmethods are not run.- Parameters:
applicationClass- this Application's subclass- Returns:
- the launched Application singleton.
- See Also:
launch(java.lang.Class<T>, java.lang.String[])
-
getInstance
public static Application getInstance()
TheApplicationsingleton, or a placeholder iflaunchhasn't been called yet.Typically this method is only called after an Application has been launched however in some situations, like tests, it's useful to be able to get an
Applicationobject without actually launching. The placeholder Application object provides access to anApplicationContextsingleton and has the same semantics as launching an Application defined like this:public class PlaceholderApplication extends Application { public void startup() { } } Application.launch(PlaceholderApplication.class);- Returns:
- the Application singleton or a placeholder
- See Also:
launch(java.lang.Class<T>, java.lang.String[]),getInstance(Class)
-
show
public void show(View view)
-
hide
public void hide(View view)
-
-