如何防止JOptionPane.showConfirmDialog框关闭 [英] How to prevent JOptionPane.showConfirmDialog box from closing

查看:169
本文介绍了如何防止JOptionPane.showConfirmDialog框关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在显示带有一些输入字段的ConfirmDialog.当保存失败(验证失败)时,我想显示一个MessageDialog框,但是我不希望ConfirmDialog框消失.我该怎么做?

I'm showing a ConfirmDialog with some input fields. When save fails (validation fails) I want to show a MessageDialog box but I don't want the ConfirmDialog box to disappear. How do I do this?

下面是我用来打开ConfirmDialog的actionPerformed方法(当我单击带有该方法的按钮作为事件处理程序时)

Below is my actionPerformed method to open the ConfirmDialog (when I click a button with this method as it's event handler)

@Override
public void actionPerformed(ActionEvent e) {

    int result = JOptionPane.showConfirmDialog(null, panel, "New transaction",
        JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
    if (result == JOptionPane.OK_OPTION) {

        // save transaction
        Transaction transaction = new Transaction();

        // ...

        if (transaction.save()) {
            // close the ConfirmDialog is OK, save was successful
        } else {

            // don't close the ConfirmDialog, save failed

            JOptionPane.showMessageDialog(panel, "Please fix the errors");
        } 
    }
}

推荐答案

您不能使用JOptionPane类的静态方法来创建对话框.

You can't use the static methods of the JOptionPane class to create the dialog.

您需要创建自己的对话框,然后将JOptionPane用作对话框的contentPane.

You need to create your own dialog and then use the JOptionPane as the contentPane of your dialog.

停止自动对话框关闭上阅读Swing教程中的部分例如有关如何完成此操作的代码.

Read the section from the Swing tutorial on Stopping Automatic Dialog Closing for example code on how this can be done.

这篇关于如何防止JOptionPane.showConfirmDialog框关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆