Eric Bergman-Terrell's Blog

Java Programming Tip: Make a JFace Dialog remember its size and position
December 11, 2008

One can easily set up JFace dialogs to remember their size and position. Furthermore, if the dialog was previously partially off the screen, it will be moved to be fully visible. Just do the following:

  1. Add a DialogSettings member
  2. Implement getDialogBoundsSettings() that returns the DialogSettings object.
  3. Implement getDialogBoundsStrategy() and have it return DIALOG_PERSISTLOCATION | DIALOG_PERSISTSIZE.
  4. Override close(). After the call to super.close(), call the DialogSetings object's save method.
  5. In your dialog's constructor, instantiate the DialogSettings object and call its load method.

For example:

package mainPackage;

import java.io.IOException;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.DialogSettings;
import org.eclipse.jface.dialogs.IDialogSettings;
...

public class ExampleDialog extends Dialog {
private final String settingsFilename = "ExampleDialog.txt";

DialogSettings dialogSettings;

    protected IDialogSettings getDialogBoundsSettings(){
        return dialogSettings;
    }

    protected int getDialogBoundsStrategy(){
        return DIALOG_PERSISTLOCATION | DIALOG_PERSISTSIZE;
    }

@Override
public boolean close() {
    boolean result = super.close();
    
    try {
dialogSettings.save(settingsFilename);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return result;
}

public ExampleDialog(Shell parentShell) {
super(parentShell);

dialogSettings = new DialogSettings("settings");

try {
dialogSettings.load(settingsFilename);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

this.setShellStyle(getShellStyle() | (SWT.RESIZE | SWT.SHELL_TRIM));
}
}
Keywords: Java, Eclipse, JFace, Dialog, SWT

Reader Comments

Comment on this Blog Post

Recent Posts

TitleDate
Java Programming Tip: SWT Photo Frame ProgramOctober 31, 2016
Vault 3 (Desktop) Version 1.63 ReleasedSeptember 9, 2016
"Compliance with Court Orders Act of 2016"April 9, 2016
Disable "Visual Voicemail" on Android / T-MobileJanuary 17, 2016
IPv6 HumorDecember 10, 2015
Java Programming Tip: Specify the JVM time zoneDecember 7, 2015
Node.js / Express Programming Tip: Detect and Fix Memory LeaksOctober 27, 2015