Is there an analogue to TextListener/TextEvent (of AWT's TextField) for JTextField?

Scott Stanchfield

But of course...

Swing uses a Model-View-Controller paradigm. Rather than watching the "component" for changes, you need to watch its model

For example:

JTextField f = new JTextField();

Document doc = f.getDocument();

  new DocumentListener() {
    public void changedUpdate(DocumentEvent e) {
      // when text/attributes change...
    public void insertUpdate(DocumentEvent e) {
      // when text inserted...
    public void removeUpdate(DocumentEvent e) {
      // when text removed...

Whenever the textfield contents are changed or the document is changed programmatically, you'll receive notification in your listener.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact