Java equivalent to C# TextBox TextChanged event

≡放荡痞女 提交于 2019-12-01 06:03:59

For Swing, If you wanted to be notified after the text component's text had changed, you'd use a DocumentListener that was added to the JTextComponent's Document. e.g.,

  JTextField myField = new JTextField();

  myField.getDocument().addDocumentListener(new DocumentListener() {

     public void removeUpdate(DocumentEvent e) {
        // TODO add code!

     }

     public void insertUpdate(DocumentEvent e) {
        // TODO add code!

     }

     public void changedUpdate(DocumentEvent e) {
        // TODO add code!

     }
  });

If on the other hand, you wanted to check text before it has been committed to the text component, you'd add a DocumentFilter to the JTextComponent's Document.

I recommend registering a DocumentListener to your component's document. Therein, you'll listen for DocumentEvents.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!