I'm working on a project and want to open the window to the position the same as last time the window was closed, and open the window the same size as last time was closed also. The rough script is as below:

When user open the window and resize it, and then close the window, next time open it again, I want the window to be opened the position which was last closed, and size the same as last time closed.

public class BrowserGUI extends Shell{

	protected static int winX = 0;  //X position of the window
	protected static int winY = 0;  //Y position of the window
	protected static int winWidth = 800;  //Window width
	protected static int winHeight = 600; //Window height
	
	
	
	public static void main(String[] args) {
		Display display = Display.getDefault();
		Shell shell = new Shell(display);
		shell.setLayout(new GridLayout());
		new BrowserGUI();
		Rectangle rect = display.getBounds();
		int screenWidth = rect.width;
		int screenHeight = rect.height;
		winX = (screenWidth-winWidth)/2;
		winY = (screenHeight-winHeight)/2;
		shell.setBounds(winX, winY, winWidth, winHeight);
		shell.open();
		while(!shell.isDisposed()){
			if(!display.readAndDispatch()) display.sleep();
		}
		display.dispose();	
	}
	
	public void finalize(){
		Rectangle rect = this.getShell().getBounds();
		winWidth = rect.width;
		winHeight = rect.height;
	}

}

Dani AI

Generated

Short version: don’t rely on finalize() — save the window bounds when the Shell is closing and restore them at startup. is right that finalize() is unreliable; is right that the coordinates must be saved before the app exits. A solid, cross‑platform approach is to persist x,y,width,height (and optionally a maximized flag) with java.util.prefs.Preferences or a small properties file, validate those values against the current monitor bounds, then set the Shell bounds at startup and write them back on close.

Example (SWT + Preferences):

Preferences prefs = Preferences.userNodeForPackage(BrowserGUI.class);

Display display = Display.getDefault();
Rectangle primary = display.getPrimaryMonitor().getBounds();

int w = prefs.getInt("winW", 800);
int h = prefs.getInt("winH", 600);
int x = prefs.getInt("winX", primary.x + (primary.width - w)/2);
int y = prefs.getInt("winY", primary.y + (primary.height - h)/2);
boolean maximized = prefs.getBoolean("maximized", false);

// clamp so window is not entirely off-screen
w = Math.max(100, Math.min(w, primary.width));
h = Math.max(100, Math.min(h, primary.height));
x = Math.max(primary.x, Math.min(x, primary.x + primary.width - w));
y = Math.max(primary.y, Math.min(y, primary.y + primary.height - h));

Shell shell = new Shell(display);
shell.setBounds(x, y, w, h);
if (maximized) shell.setMaximized(true);

shell.addShellListener(new ShellAdapter() {
    @Override public void shellClosed(ShellEvent e) {
        Rectangle b = shell.getBounds();
        prefs.putInt("winX", b.x);
        prefs.putInt("winY", b.y);
        prefs.putInt("winW", b.width);
        prefs.putInt("winH", b.height);
        prefs.putBoolean("maximized", shell.getMaximized());
        try { prefs.flush(); } catch (java.util.prefs.BackingStoreException ex) { /* log if needed */ }
    }
});

Troubleshooting tips: clamp restored bounds to the current monitor(s) so the window does not open off‑screen if the user changed displays or resolution; for multi‑monitor setups you can inspect display.getMonitors() and pick the monitor that contains the saved rectangle. If you want to preserve the “normal” size when the window was maximized, track the last non‑maximized bounds on resize events and persist those instead of the maximized rectangle.

Recommended Answers

All 5 Replies

In Java finalize() is pretty useless, mainly because you don't know when it will be called (the official answer is "sometime later, unless something causes it not to be called at all"). Better to save the coordinates before calling dispose(). Without seeing the other classes it's hard to say more.

That's all the classes i have now.

There are some errors in the codes, shall remove the "extends Shell" and remove the codes inside the finalize function.

Apart from Shell, there's also Display which is a bit of a mystery

Correct code shall be as below. I tested it, it's working. But the problem now is how to open the window the same size and position as last time it was closed.

public class BrowserGUI {

	protected static int winX = 0;  //X position of the window
	protected static int winY = 0;  //Y position of the window
	protected static int winWidth = 800;  //Window width
	protected static int winHeight = 600; //Window height
	
	
	public static void main(String[] args) {
		Display display = Display.getDefault();
		Shell shell = new Shell(display);
		shell.setLayout(new GridLayout());
		new BrowserGUI();
		Rectangle rect = display.getBounds();
		int screenWidth = rect.width;
		int screenHeight = rect.height;
		winX = (screenWidth-winWidth)/2;
		winY = (screenHeight-winHeight)/2;
		shell.setBounds(winX, winY, winWidth, winHeight);
		shell.open();
		while(!shell.isDisposed()){
			if(!display.readAndDispatch()) display.sleep();
		}
		display.dispose();	
	}
	
	public void finalize(){
		//Rectangle rect = this.getShell().getBounds();
		//winWidth = rect.width;
		//winHeight = rect.height;
	}

}

The only way would be saving the co-ordinates before closing the window..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.