【问题标题】:How to display error message in an Eclipse RCP wizard?如何在 Eclipse RCP 向导中显示错误消息?
【发布时间】:2014-11-11 15:54:43
【问题描述】:
我想显示一条错误消息,它出现在向导窗口顶部的向导中(如 Cannot create project content... 下面屏幕截图中的消息)。
根据我在网上找到的,我必须使用setErrorMessage的方法来做到这一点。
但它在我的向导类中不存在:
import org.eclipse.jface.wizard.Wizard;
public class MyWizard extends Wizard {
public MyWizard() {
super();
setErrorMessage("Error message"); // No such method
getContainer().getCurrentPage().setErrorMessage("Error message 2"); // This also doesn't exist
}
如何设置向导的错误信息?
【问题讨论】:
标签:
java
eclipse
swt
eclipse-rcp
jface
【解决方案1】:
setErrorMessage是WizardPage中的一个方法,但它不包含在IWizardContainer.getCurrentPage返回的IWizardPage接口中。
通常是您的向导页面类设置错误消息 - 他们可以调用 setErrorMessage(text)
【解决方案2】:
JFace 的Wizards 有页面。您自己创建这些页面,扩展WizardPage。在该类中,您将找到 setErrorMessage API。
更快的替代方法是使用不需要页面的TitleAreaDialog。您也可以在那里使用错误 API。
示例
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
/**
*
* @author ggrec
*
*/
public class TestWizard extends Wizard
{
// ==================== 3. Static Methods ====================
public static void main(final String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
final WizardDialog dialog = new WizardDialog(shell, new TestWizard());
dialog.open();
if (!display.readAndDispatch())
display.sleep();
display.dispose();
}
// ==================== 4. Constructors ====================
private TestWizard()
{
}
// ==================== 5. Creators ====================
@Override
public void addPages()
{
addPage(new TestPage());
// Or, you could make a local var out of the page,
// and set the error message here.
}
// ==================== 6. Action Methods ====================
@Override
public boolean performFinish()
{
return true;
}
// =======================================================
// 19. Inline Classes
// =======================================================
private class TestPage extends WizardPage
{
private TestPage()
{
super(TestPage.class.getCanonicalName());
}
@Override
public void createControl(final Composite parent)
{
setControl(new Composite(parent, SWT.NULL));
setErrorMessage("HOUSTON, WE'RE GOING DOWN !!!!!");
}
}
}