我需要一个向导,第二页内容取决于第一页的选择。第一页询问用户他想要创建的过滤器“种类”,第二页询问用户创建所选“种类”的过滤器实例。
JFace 的向导页面内容(createControl(...) 方法)都是在向导打开时创建的,而不是在显示给定页面时创建的(我猜这允许 JFace 知道向导大小??)。
因此,我必须在向导打开之前创建我的第二页内容,但我不能,因为第二页的内容取决于第一页的选择。
目前,我找到的更简洁的解决方案包括在向导打开(及其内容)之前创建所有(秒)页面,并覆盖第一页实现中的 getNextPage() 方法。
该解决方案的主要缺点是,当要创建许多第二页时,它的成本可能很高。
您如何看待该解决方案?您如何管理向导的页面?有没有我错过的更清洁的解决方案?
最佳答案
如果你是其他几个页面,这种方法是正确的
- 完全不同的一个
- 取决于之前在前一页所做的选择
然后你可以add the next page dynamically (也作为 described here )
但是如果您只有下一页包含动态内容,您应该能够在 onEnterPage()
method 中创建该内容
public void createControl(Composite parent)
{
//
// create the composite to hold the widgets
//
this.composite = new Composite(parent, SWT.NONE);
//
// create the desired layout for this wizard page
//
GridLayout layout = new GridLayout();
layout.numColumns = 4;
this.composite.setLayout(layout);
// set the composite as the control for this page
setControl(this.composite);
}
void onEnterPage()
{
final MacroModel model = ((MacroWizard) getWizard()).model;
String selectedKey = model.selectedKey;
String[] attrs = (String[]) model.macroMap.get(selectedKey);
for (int i = 0; i < attrs.length; i++)
{
String attr = attrs[i];
Label label = new Label(this.composite, SWT.NONE);
label.setText(attr + ":");
new Text(this.composite, SWT.NONE);
}
pack();
}
如 eclipse 角文章Creating JFace Wizards :
我们可以通过覆盖任何向导页面的 getNextPage 方法来更改向导页面的顺序。在离开页面之前,我们将用户选择的值保存在模型中。在我们的示例中,根据旅行的选择,用户接下来会看到航类页面或汽车旅行页面。
public IWizardPage getNextPage(){
saveDataToModel();
if (planeButton.getSelection()) {
PlanePage page = ((HolidayWizard)getWizard()).planePage;
page.onEnterPage();
return page;
}
// Returns the next page depending on the selected button
if (carButton.getSelection()) {
return ((HolidayWizard)getWizard()).carPage;
}
return null;
}
我们定义了一个方法来对PlanePage
进行初始化,onEnterPage()
并且我们在移动到PlanePage
时调用这个方法code>,即在第一页的 getNextPage()
方法中。
关于java - Eclipse JFace 的向导,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/954443/