Author Topic: [Java] Save window size/position  (Read 6455 times)

0 Members and 2 Guests are viewing this topic.

Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
[Java] Save window size/position
« 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();
}

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!