java – Eclipse:以编程方式创建首选项页面

我正在尝试以编程方式创建首选项页面,我需要使用首选项页面而不在plugin.xml中定义preferencePage扩展点
我非常接近解决方案,我能够加载页面并在第一次加载应用程序时保存值,

我的代码的核心是

PreferenceManager pmngr= PlatformUI.getWorkbench().getPreferenceManager();
 //this come from other plugins that implements my personal IPreferences 
    PreferencePageRCP page =new PreferencePageRCP((IPreferences) element.createExecutableExtension("class"));

    PreferenceNodeRCP node= new PreferenceNodeRCP(page.getId(), page.getTitle(),null,PreferencePageRCP.class.getName());

    node.setPage(page);
     pmngr.addToRoot(node);

其中PreferencePageRCP是我的自定义首选项页面所以这一点我让我的PreferencePage工作!

但是当我第二次进入首选项窗口时,我在PreferenceNode.createPage上出现错误,所以现在我自己的PreferenceNode类重写了createPage但是现在我遇到了一个UI错误

Problems occurred when invoking code from plug-in: "org.eclipse.jface".
!STACK 0
org.eclipse.swt.SWTException: Widget is disposed
 at org.eclipse.swt.SWT.error(SWT.java:4083)
 at org.eclipse.swt.SWT.error(SWT.java:3998)
 at org.eclipse.swt.SWT.error(SWT.java:3969)
 at org.eclipse.swt.widgets.Widget.error(Widget.java:468)
 at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:340)
 at org.eclipse.swt.widgets.Control.setVisible(Control.java:3370)
 at org.eclipse.jface.dialogs.DialogPage.setVisible(DialogPage.java:470)
 at org.eclipse.jface.preference.FieldEditorPreferencePage.setVisible(FieldEditorPreferencePage.java:374)
 at org.eclipse.jface.preference.PreferenceDialog.showPage(PreferenceDialog.java:1323)
 at org.eclipse.ui.internal.dialogs.FilteredPreferenceDialog.showPage(FilteredPreferenceDialog.java:673)
 at org.eclipse.jface.preference.PreferenceDialog$10.run(PreferenceDialog.java:708)
 at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70)
 andContributionItem.java:796
.................

所以第二次UI中缺少一些东西
我在这一点上我无法修复我的代码,有人成功地以编程方式创建了首选项页面???

解决方法:

这是java代码,它允许以编程方式创建首选项页面:


//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);

//use workbenches's preference manager PreferenceManager pm= PlatformUI.getWorkbench().getPreferenceManager();

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 which preference-store it should write to pd.setPreferenceStore(Activator.getDefault().getPreferenceStore()); pd.create(); pd.open();

上一篇:Flutter 数据存储之 shared_preferences


下一篇:android – 如何在首选项更改时重新安排Alarm Manager