Package examples

Class SessionStorageExample1


public class SessionStorageExample1 extends Application
An example that demonstrates the default support for saving and restoring GUI session state. Try running the application, resizing and moving the main Frame, resizing some of the color split panes, changing the selected tab, the widths of columns in the "all colors" tab. When the app is restarted, those GUI features should be restored to the way you left them.

When the application exits, session state for the application's mainFrame component tree is saved using the SessionStorage

invalid reference
save
method, and when the application is launched it's restored with the
invalid reference
restore
method. This is done by overriding the Application startup and shutdown methods:
 @Override protected void shutdown() {
     getContext().getSessionStorage().save(mainFrame, "session.xml");
 }
 @Override protected void startup() {
     ApplicationContext ctx = getContext();
     ctx.setVendorId("Sun");
     ctx.setApplicationId("SessionStorageExample1");
     // ... create the GUI rooted by JFrame mainFrame
     ctx.getSessionStorage().restore(mainFrame, "session.xml");
 }
 
Error handling has been ommitted from the example.

Session state is stored locally, relative to the user's home directory, using 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, the full pathname for filename "session.xml" is:

 ${userHome}\Application Data\${vendorId}\${applicationId}\session.xml
 
Where the value of ${userHome} is the the value of the Java System property "user.home".

Note: this example is intended to show how the SessionStorage API works and what it can do. Applications subclasses, like SingleFrameApplication, save/restore session state automatically, so you don't have to.

See Also:
  • invalid reference
    application.SessionStorage#save
  • invalid reference
    application.SessionStorage#restore
  • ApplicationContext.getSessionStorage()
  • invalid reference
    ApplicationContext#setApplicationId
  • invalid reference
    ApplicationContext#setVendorId