JOptionPane.showMessageDialog等到单击确定吗? [英] JOptionPane.showMessageDialog wait until OK is clicked?

查看:99
本文介绍了JOptionPane.showMessageDialog等到单击确定吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是我忽略的非常简单的事情,但是我似乎无法弄清楚.

This might be a very simple thing that I'm overlooking, but I just can't seem to figure it out.

我具有以下更新JTable的方法:

I have the following method that updates a JTable:

class TableModel extends AbstractTableModel {    
        public void updateTable() {
            try {
                // update table here
             ...
    } catch (NullPointerException npe) {
                isOpenDialog = true;
                JOptionPane.showMessageDialog(null, "No active shares found on this IP!");
                isOpenDialog = false;
            }
        }
    }

但是,我不希望在按下消息对话框上的确定"按钮之前将isOpenDialog布尔值设置为false,因为如果用户按下Enter键,它将激活文本字段上的KeyListener事件并触发如果将整个代码块设置为false.

However, I don't want isOpenDialog boolean to be set to false until the OK button on the message dialog is pressed, because if a user presses enter it will activate a KeyListener event on a textfield and it triggers that entire block of code again if it's set to false.

部分KeyListener代码如下所示:

Part of the KeyListener code is shown below:

public class KeyReleased implements KeyListener {
        ...

    @Override
    public void keyReleased(KeyEvent ke) {
        if(txtIPField.getText().matches(IPADDRESS_PATTERN)) {
            validIP = true;
        } else {
            validIP = false;
        }

        if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
            if (validIP && !isOpenDialog) {
                updateTable();
            }
        }
    }
}

JOptionPane.showMessageDialog()是否有某种机制可以阻止在按下OK按钮之前执行下一行?谢谢.

Does JOptionPane.showMessageDialog() have some sort of mechanism that prevents executing the next line until the OK button is pressed? Thank you.

推荐答案

JOptionPane创建了一个模式对话框,因此在处理完该对话框之前,将不会按设计方式调用它之外的行(两个按钮之一按下或按下关闭菜单按钮).

The JOptionPane creates a modal dialog and so the line beyond it will by design not be called until the dialog has been dealt with (either one of the buttons have been pushed or the close menu button has been pressed).

更重要的是,您不应该在这种情况下使用KeyListener.如果要让JTextField侦听Enter键的按下,请向其中添加一个ActionListener.

More important, you shouldn't be using a KeyListener for this sort of thing. If you want to have a JTextField listen for press of the enter key, add an ActionListener to it.

这篇关于JOptionPane.showMessageDialog等到单击确定吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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