Search code examples
javaswingdialogjoptionpane

Custom JOptionPane Get "Cancel" action when overriding YES_NO_CANCEL buttons


I have a small problem trying to figure out how to check if a user presses a button in a custom JOptionPane.

My dialog is based on an inputDialog with custom texts for the YES, NO and CANCEL buttons ("Select", "Cancel", "Open Editor").

I tried searching for a solution, but all I found was questions that used the static JOptionPane functions.

Here is my code I am using for now:

public SelectItemDialog(Component parent) {
    super("Please select an item:", YES_NO_CANCEL_OPTION, PLAIN_MESSAGE, Editor.getIcon("bookmark"),
        new String[] { "Select", "Cancel", "Open Item Editor" }, "Select"
    );

    setWantsInput(true);

    setSelectionValues(null); // Would replace with an Object array
    setInitialSelectionValue(null);

    setComponentOrientation(getRootFrame().getComponentOrientation());

    JDialog dialog = createDialog(parent, "Select Item");
    selectInitialValue();

    dialog.setVisible(true);
    dialog.dispose();

    Object obj = getInputValue();

    if(obj instanceof Item) {
        this.openEditor = false;
        this.item = (Item) obj;

    } else {
        this.openEditor = (obj.equals( CANCEL_OPTION));
        this.item = null;
    }
}

The check for CANCEL_OPTION is not working at all, same with UNDEFINED_OPTION.

Any ideas?


Solution

  • Actually I just had to use the Object returned by the JOptionPane itself: getValue(), problem solved!