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
Node.js + Express: How to Block Requests by User-Agent HeadersJanuary 7, 2026
Vault 3 is Now Available for Windows on ARM Machines!December 13, 2025
Vault 3: How to Include Outline Text in Exported PhotosOctober 26, 2025
.NET Public-Key (Asymmetric) Cryptography DemoJuly 20, 2025
Raspberry Pi 3B+ Photo FrameJune 17, 2025
EBTCalc (Android) Version 1.53 is now availableMay 19, 2024
Vault 3 Security EnhancementsOctober 24, 2023