自定义JOptionPane重写YES_NO_CANCEL按钮时获取“取消”操作

时间:2013-07-24 16:27:58

标签: java swing dialog joptionpane

我在尝试弄清楚如何检查用户是否按下自定义JOptionPane中的按钮时遇到了一个小问题。

我的对话框基于一个inputDialog,其中包含YES,NO和CANCEL按钮的自定义文本(“选择”,“取消”,“打开编辑器”)。

我尝试寻找解决方案,但我发现的只是使用静态JOptionPane函数的问题。

这是我现在使用的代码:

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;
    }
}

对CANCEL_OPTION的检查根本不起作用,与UNDEFINED_OPTION相同。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

实际上我只需要使用JOptionPane本身返回的Object:getValue(),问题解决了!