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
EBTCalc (Android) Version 1.53 is now availableMay 19, 2024
Vault 3 Security EnhancementsOctober 24, 2023
Vault 3 is now available for Apple OSX M2 Mac Computers!September 18, 2023
Vault (for Desktop) Version 0.77 ReleasedMarch 26, 2023
EBTCalc (Android) Version 1.44 is now availableOctober 12, 2021
Vault (Desktop) Version 0.72 ReleasedOctober 6, 2021
EBT Compass is Now Available for Android DevicesJune 2, 2021