Maximizing a JFace dialog programmatically

A short snippet to show you how to maximize a JFace dialog.
If you have googled it, you must have heard about the method setMaximized of the Shell class. However, you have to call this method at the right place in your code. Otherwise, it will not work.

The secret is to invoke this method on the dialog’s shell only once this shell has been created. You cannot configure it before. I have struggled a long time to understand why Shell#setMaximized did not work when invoked from Dialog#createContents(Composite). At least, it does not work on Windows 7. Eventually, I found it. The solution is to invoke it in Dialog#constrainShellSize().

public class ApplicationUI extends ApplicationWindow {

	public ApplicationUI() {
		super( null );
		// ...
	}

	@Override
	protected void constrainShellSize() {
		super.constrainShellSize();
		getShell().setMaximized( true );
	}

	// ...
}

About this entry