开发者

JOption pane checking if some input text was enter, if not OK button not working

in JOption pane I have some JTextArea where user must eneter some text. Then if user click OK code execute. But i want that code will not execute if input text is not enetered.

I am using mouse listener to check OK button but i do not know how to reopen JOption if input text is not enetered and let him go to next step if is text enetered.

my code:

    case JOptionPane.YES_OPTION:  {

        String text= op.getText();
        if (text.equals("")) {
          开发者_如何学编程  // how to prevent closing joptionpane!
        } else {
            // my code
            break;
        }
    }


JTextArea txt = new JTextArea();
JButton ok = new JButton("OK");

ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                if (txt.getText().toString().trim().length() == 0 ) {
                }
                else {
                    //your action...
                }
            }
        });


By setting the event property of the listener programmticaly to true, you can "catch" the click and prevent the dialog from closing. Simply check if the TextArea is empty in the click event and if yes, set handled to true.

Further you should provide a information to the user afterwards, which field needs to be filled.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜