1 package au.gov.amsa.util.swing; 2 3 import java.awt.event.WindowAdapter; 4 import java.awt.event.WindowEvent; 5 import java.util.prefs.Preferences; 6 7 import javax.swing.JFrame; 8 9 public final class FramePreferences { 10 11 public static void restoreLocationAndSize(JFrame frame, int defaultX, int defaultY, 12 int defaultWidth, int defaultHeight, Class<?> cls) { 13 Preferences prefs = Preferences.userNodeForPackage(cls); 14 int x = Integer.parseInt(prefs.get("frame.x", defaultX + "")); 15 int y = Integer.parseInt(prefs.get("frame.y", defaultY + "")); 16 int width = Integer.parseInt(prefs.get("frame.width", defaultWidth + "")); 17 int height = Integer.parseInt(prefs.get("frame.height", defaultHeight + "")); 18 frame.setLocation(x, y); 19 frame.setSize(width, height); 20 frame.addWindowListener(new WindowAdapter() { 21 22 @Override 23 public void windowClosing(WindowEvent e) { 24 prefs.put("frame.x", frame.getX() + ""); 25 prefs.put("frame.y", frame.getY() + ""); 26 prefs.put("frame.width", frame.getWidth() + ""); 27 prefs.put("frame.height", frame.getHeight() + ""); 28 } 29 30 }); 31 32 } 33 34 }