Document: addDocumentListener(DocumentListener listener) : Document « javax.swing.text « Java by API






Document: addDocumentListener(DocumentListener listener)

 


import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;

public class Main {
  JButton button = new JButton("foo");
  JTextField textfield = new JTextField(10);
  Document document;

  public Main() {
    document = textfield.getDocument();
    document.addDocumentListener(new JButtonStateController());
  }
  class JButtonStateController implements DocumentListener {
    JButtonStateController() {
    }

    public void changedUpdate(DocumentEvent e) {
      disableIfEmpty(e);
    }

    public void insertUpdate(DocumentEvent e) {
      disableIfEmpty(e);
    }

    public void removeUpdate(DocumentEvent e) {
      disableIfEmpty(e);
    }

    public void disableIfEmpty(DocumentEvent e) {
      button.setEnabled(e.getDocument().getLength() > 0);
    }
  }
}

   
  








Related examples in the same category

1.Document: addUndoableEditListener(UndoableEditListener listener)
2.Document: dump(PrintStream out)
3.Document: getDefaultRootElement()
4.Document: getLength()
5.Document: getText(int offset, int length)
6.Document: insertString(int offset, String str, AttributeSet a)
7.Document: remove(int offs, int len)