Save Window Settings #37
Chapter 5, Windows, Dialogs, and Frames
|
193
HACK
—Jonathan Simon
H A C K
#37
Save Window Settings Hack #37
Make sure your windows always show up right where you left them, even
after a program restarts, by saving the window position and size
automatically.
Swing is a rich toolkit that can be used to create many kinds of programs,
but there are certain features that virtually all applications need, like win-
dow settings and preferences. This hack shows how to automatically store
and retrieve window locations and dimensions in an existing program with-
out using custom
Frame subclasses, or even making many changes to your
existing code.
Saving the size and location of a window is actually pretty easy. You can just
store them in a file and retrieve them later. The difficulty is identifying each
window, and doing it in a way that’s as noninvasive as possible.
The Window Saver Class
The first step is to create a class that handles all of the work. Because man-
aging windows will be a global function of the entire program, start with a
simple singleton with a factory interface, as shown in Example 5-4.
JLabel dateLabel = new JLabel("12/31/99");
dateLabel.setHorizontalAlignment(SwingConstants.CENTER);
statusBar.addRightComponent(dateLabel, 30);
JLabel timeLabel = new JLabel("11:59 PM");
timeLabel.setHorizontalAlignment(SwingConstants.CENTER);
statusBar.addRightComponent(timeLabel, 30);
contentPane.add(statusBar, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show( );
}
}
Example 5-4. Singleton for saving window settings
public class WindowSaver implements AWTEventListener {
private static WindowSaver saver;
private Map framemap;
Example 5-3. Using the JStatusBar in a JFrame (continued)
194
|
Chapter 5, Windows, Dialogs, and Frames
#37 Save Window Settings
HACK
The
WindowSaver
constructor creates a private map to store all of an applica-
tion’s frames. When each frame is loaded, the saver will store a reference to it
in this map. Later, when the application needs to save or reload each window,
it will use the map to find each frame again. The constructor is private so that
only one instance (and one map) of the program ever exists in the JVM.
The
WindowSaver also implements AWTEventListener. This is how it can find
each frame. Swing has a global event queue that allows you to get every
event throughout the JVM. You can access this queue by registering an
AWTEventListener to the global toolkit like this:
Toolkit tk = Toolkit.getDefaultToolkit( );
tk.addAWTEventListener(WindowSaver.getInstance( ),
AWTEvent.WINDOW_EVENT_MASK);
AWTEventListener defines a single method, eventDispatched(AWTEvent).
Through this method, you can look for each window as it is opened.
Each window produces several events for actions such as closing, opening,
resizing, hiding, etc. Some events, such as activation, happen each time a
window is shown on the screen. The
WINDOW_OPEN event will be called only
once per window, right when the window is first shown. This is perfect for
our purposes because that particular event will happen after an application
has created and initialized each window, but before the window is shown on
screen, making it the ideal place to set window size and location:
public void eventDispatched(AWTEvent evt) {
try {
if(evt.getID( ) == WindowEvent.WINDOW_OPENED) {
ComponentEvent cev = (ComponentEvent)evt;
if(cev.getComponent( ) instanceof JFrame) {
JFrame frame = (JFrame)cev.getComponent( );
loadSettings(frame);
}
}
}catch(Exception ex) {
p(ex.toString( ));
}
}
private WindowSaver( ) {
framemap = new HashMap( );
}
public static WindowSaver getInstance( ) {
if(saver == null) {
saver = new WindowSaver( );
}
return saver;
}
Example 5-4. Singleton for saving window settings (continued)

Get Swing Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.