Value Change Listener to JTextField

前端 未结 12 1973
情歌与酒
情歌与酒 2020-11-22 04:16

I want the message box to appear immediately after the user changes the value in the textfield. Currently, I need to hit the enter key to get the message box to pop out. Is

12条回答
  •  花落未央
    2020-11-22 04:28

    textBoxName.getDocument().addDocumentListener(new DocumentListener() {
       @Override
       public void insertUpdate(DocumentEvent e) {
           onChange();
       }
    
       @Override
       public void removeUpdate(DocumentEvent e) {
          onChange();
       }
    
       @Override
       public void changedUpdate(DocumentEvent e) {
          onChange();
       } 
    });
    

    But I would not just parse anything the user (maybe on accident) touches on his keyboard into an Integer. You should catch any Exceptions thrown and make sure the JTextField is not empty.

提交回复
热议问题