Inondle
Inondle

Reputation: 360

How to add buttons to the JFace ErrorDialog

I'm trying to add a "Cancel" button to this popup dialog, the dialog basically just gives the user some info and allows them to hit Yes or view details. The problem is that there is no Cancel button and I would like to add one.

The dialog is a JFace ErrorDialog which uses a premade MultiStatus to display the error message. The dialog opens and gives an OK button or a Cancel button. Is there anyway to directly manipulate how the dialog creates buttons or some other method I could use to change how it looks? Any help is appreciated!

if (ErrorDialog.openError(shell, 
    Messages.ConsistencyAction_confirm_dialog_title, null,
    multiStatus, IStatus.WARNING) != Window.OK) {
    return;
}

This is the dialog I'm trying to change. This is basically checking to make sure that someone presses ok, if they don't then you exit. You can exit it by hitting the red X in the corner but it'd be less confusing to have a button.

Upvotes: 2

Views: 309

Answers (1)

greg-449
greg-449

Reputation: 111216

You can extend the ErrorDialog class so that you can override the createButtonsForButtonBar method.

For example this is from the Eclipse p2 install plugin:

public class OkCancelErrorDialog extends ErrorDialog {

    public OkCancelErrorDialog(Shell parentShell, String dialogTitle, String message, IStatus status, int displayMask) {
        super(parentShell, dialogTitle, message, status, displayMask);
    }

    @Override
    protected void createButtonsForButtonBar(Composite parent) {
        // create OK, Cancel and Details buttons
        createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
        createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, true);
        createDetailsButton(parent);
    }
}

With this you can't use the static ErrorDialog.openError method, instead you will have to do something like:

OkCancelErrorDialog dialog = new OkCancelErrorDialog(shell, Messages.ConsistencyAction_confirm_dialog_title, null, multiStatus, IStatus.WARNING);

Upvotes: 4

Related Questions