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;
	}

}

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.