Custom buttons in DialogWrapper

Answered

Hello,

I need to replace OK and Cancel buttons in Dialog wrapper with my own buttons. For now I just renamed OK/Cancel but I will need to add more buttons in the same line.

So can you please help with the following questions:

- Is it possible to remove OK/Cancel buttons from dialog (after that I can use createButtonsPanel())

- Is it possible to add more buttons in the same line as OK and Cancel buttons?

There was similar question but I didn't find desired in WizardDialog - https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000651864-DialogWrapper-with-different-buttons

Thank you in advance!

4 comments
Comment actions Permalink

You should override button creation methods:

import com.intellij.openapi.ui.DialogWrapper;
import org.jetbrains.annotations.NotNull;

import javax.swing.Action;
import java.awt.event.ActionEvent;

class MyDialog extends DialogWrapper {
    private class MyCustomAction extends DialogWrapperAction {
        protected MyCustomAction() {
            super("Label");
            putValue(Action.NAME, "Label");
        }

        @Override
        protected void doAction(ActionEvent e) {
            if (doValidate() == null) {
                getOKAction().setEnabled(isOkEnabled());
            }
            // set implementation specific values to signal that this custom button was the cause for closing the dialog 
            // .....
            doOKAction();
        }
    }

    boolean isOkEnabled() {
        // return true if dialog can be closed
        return true;
    }

    @NotNull
    @Override
    protected Action[] createActions() {
        super.createDefaultActions();
        // return right hand side action buttons
        return new Action[] { myPastePlainTextAction, myPasteHtmlAction, myOkAction, getCancelAction() };
    }

    @NotNull
    protected Action[] createLeftSideActions() {
        // return left hand side action buttons
        return new Action[] { copyToDefaultsAction, copyFromDefaultsAction, resetAction, };
    }
}

0
Comment actions Permalink

You can override `com.intellij.openapi.ui.DialogWrapper#createActions` (and `com.intellij.openapi.ui.DialogWrapper#createLeftSideActions`).

See "Tip of the day" dialog as an example:
https://github.com/JetBrains/intellij-community/blob/master/platform/platform-impl/src/com/intellij/ide/util/TipDialog.java#L76

0
Comment actions Permalink

Aleksey Pivovarov hello, could you be more clear? Maybe with an example. I need to change the name of the button and implement an action. How can I do? Thanks

0
Comment actions Permalink

Sorry, I'm not sure what might be added to 2 answers above.
Maybe, share your code and where it doesn't work as expected?

0

Please sign in to leave a comment.