简体   繁体   中英

How to display error message in an Eclipse RCP wizard?

I want to display an error message, which appear in a wizard at the top of the wizard window (like Cannot create project content... message in the screenshot below).

截图

According to what I found on the internets, I have to use the method setErrorMessage to do this.

But it doesn't exist in my wizard class:

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
    }

How can I set the error message of a wizard?

JFace's Wizard s have pages. You create these pages yourself, extending WizardPage . In that class you will find the setErrorMessage API.

A faster alternative would be to use a TitleAreaDialog , which doesn't require pages. You can use the error API there as well.


Example

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 !!!!!");
        }

    }

}

setErrorMessage is a method in WizardPage but it is not included in the IWizardPage interface that IWizardContainer.getCurrentPage returns.

It is usually your wizard page classes that set the error message - which they can do be calling setErrorMessage(text)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM