Class SessionStorage

java.lang.Object
org.jdesktop.application.SessionStorage

public class SessionStorage extends Object
Support for storing GUI state that persists between Application sessions.

This class simplifies the common task of saving a little bit of an application's GUI "session" state when the application shuts down, and then restoring that state when the application is restarted. Session state is stored on a per component basis, and only for components with a name and for which a SessionState.Property object has been defined. SessionState Properties that preserve the bounds Rectangle for Windows, the dividerLocation for JSliderPanes and the selectedIndex for JTabbedPanes are defined by default. The ApplicationContext getSesssionStorage method provides a shared SessionStorage object.

A typical Application saves session state in its shutdown() method, and then restores session state in startup():

 public class MyApplication extends Application {
     @Override protected void shutdown() {
         getContext().getSessionStorage().save(mainFrame, "session.xml");
     }
     @Override protected void startup() {
         ApplicationContext appContext = getContext();
         appContext.setVendorId("Sun");
         appContext.setApplicationId("SessionStorage1");
         // ... create the GUI rooted by JFrame mainFrame
         appContext.getSessionStorage().restore(mainFrame, "session.xml");
     }
     // ...
 }
 
In this example, the bounds of mainFrame as well the session state for any of its JSliderPane or JTabbedPane will be saved when the application shuts down, and restored when the applications starts up again. Note: error handling has been omitted from the example.

Session state is stored locally, relative to the user's home directory, by the LocalStorage save and load methods. The startup method must set the ApplicationContext vendorId and applicationId properties to ensure that the correct local directory is selected on all platforms. For example, on Windows XP, the full pathname for filename "session.xml" is typically:

 ${userHome}\Application Data\${vendorId}\${applicationId}\session.xml
 
Where the value of ${userHome} is the the value of the Java System property "user.home". On Solaris or Linux the file is:
 ${userHome}/.${applicationId}/session.xml
 
and on OSX:
 ${userHome}/Library/Application Support/${applicationId}/session.xml
 
See Also: