How to Create a Preference Page in Eclipse Rcp

Objective: -To create an eclipse-rcp preference page without using the preference extension, all is done in code. This is done by creating ...

-To create an eclipse-rcp preference page without using the preference extension, all is done in code. This is done by creating a customized button with a SelectionListener, and eventually that action will call a customized preference page.

How to do:
1.) create a new eclipse-rcp application with a view
2.) add a button in the view's createControl method
    -this button should have a SelectionListener attached which will be called onclick
public void createPartControl(Composite parent) {
    Button b = new Button(parent, SWT.NONE);
    b.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            //create an instance of the custom MyPreference class
            IPreferencePage page = new MyPreference(); 
            page.setTitle("Custom Configurations");
            //create a new PreferenceNode that will appear in the Preference window
            PreferenceNode node = new PreferenceNode("1", page); 
            PreferenceManager pm = new PreferenceManager();
            pm.addToRoot(node); //add the node in the PreferenceManager
            Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
            //instantiate the PreferenceDialog
            PreferenceDialog pd = new PreferenceDialog(shell, pm); 
            //this line is important, it tell's the PreferenceDialog on what store to used

MyPreference class
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.irri.cril.jicis.administrator.Activator;

public class MyPreference extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
    public MyPreference() {
    public void init(IWorkbench workbench) {
        setDescription("Custom Configuration");

    protected void createFieldEditors() {
        //FieldEditor types FieldEditor fields here
        addField(new StringFieldEditor("NAME", "Name:", getFieldEditorParent()));

This code can now save and retrieve values in your default preference store. if you want to override the ok, cancel, default, etc actions, you can add on the MyPreference class.


java-eclipse-rcp 3792667745086813164

Post a Comment Default Comments

1 comment

Skip said...

Thank you, it worked!


Are you looking for freelancers in the Philippines? Get in touch.


If you like what I do, you can support this channel by buying me a coffee. I would be grateful for your contribution! Your donations will help me create more FREE online courses and learning materials for programmers and remote workers.