Java equivalent to C# TextBox TextChanged event

后端 未结 2 1416
借酒劲吻你
借酒劲吻你 2021-01-13 02:35

in C# there is an event for textboxes as follows

private void fooText_TextChanged(object sender, EventArgs e)
{
    //do something 
}

the

2条回答
  •  -上瘾入骨i
    2021-01-13 03:22

    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.

提交回复
热议问题