Clan x86

Technical (Development, Security, etc.) => General Programming => Tutorials, References, and Examples => Topic started by: Camel on November 01, 2007, 01:02:35 pm

Title: [Java] Save window size/position
Post by: Camel on November 01, 2007, 01:02:35 pm
This is based off of something I found somewhere on the internet, but I didn't write the source down, so they won't get any credit.

Code: [Select]
        // Globally save/load window positions
Toolkit.getDefaultToolkit().addAWTEventListener(
new AWTEventListener() {
public void eventDispatched(AWTEvent event) {
WindowEvent wev = (WindowEvent)event;
Window window = (Window)wev.getComponent();
switch(event.getID()) {
case WindowEvent.WINDOW_CLOSING:
case WindowEvent.WINDOW_CLOSED:
savePosition(window);
break;
case WindowEvent.WINDOW_OPENED:
loadPosition(window);
break;
}
}},
AWTEvent.WINDOW_EVENT_MASK);

Code: [Select]
private static void loadPosition(Window w) {
String header = w.getClass().getSimpleName();
Rectangle bounds = w.getBounds();
if((w instanceof Frame) && ((Frame)w).isResizable()) {
bounds.height = Integer.valueOf(Settings.read(header, "height", Integer.toString(bounds.height)));
bounds.width = Integer.valueOf(Settings.read(header, "width", Integer.toString(bounds.width)));
}
bounds.x = Integer.valueOf(Settings.read(header, "x", Integer.toString(bounds.x)));
bounds.y = Integer.valueOf(Settings.read(header, "y", Integer.toString(bounds.y)));
w.setBounds(bounds);
if(w instanceof GuiDesktop) {
dividerLocation = Integer.valueOf(Settings.read(header, "dividerLocation", "550"));
for(GuiEventHandler gui : guis)
gui.setDividerLocation(dividerLocation);
}
}

protected static void savePosition(Window w) {
String header = w.getClass().getSimpleName();
Rectangle bounds = w.getBounds();
if((w instanceof Frame) && ((Frame)w).isResizable()) {
Settings.write(header, "height", Integer.toString(bounds.height));
Settings.write(header, "width", Integer.toString(bounds.width));
}
Settings.write(header, "x", Integer.toString(bounds.x));
Settings.write(header, "y", Integer.toString(bounds.y));
if((w instanceof GuiDesktop) && (selectedGui != null))
Settings.write(header, "dividerLocation", Integer.toString(selectedGui.getDividerLocation()));
Settings.store();
}