Java JTextComponent addChangeListener(JTextComponent text, ChangeListener changeListener)

Here you can find the source of addChangeListener(JTextComponent text, ChangeListener changeListener)

Description

Installs a listener to receive notification when the text of any JTextComponent is changed.

License

Open Source License

Parameter

Parameter Description
text any text component, such as a JTextField or JTextArea
changeListener a listener to receieve ChangeEvent s when the text is changed; the source object for the events will be the text component

Exception

Parameter Description
NullPointerException if either parameter is null

Declaration

public static void addChangeListener(JTextComponent text, ChangeListener changeListener) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import java.beans.PropertyChangeEvent;
import java.util.Objects;

public class Main {
    /**/*from ww  w. j  a  v a  2 s. c  o m*/
     * Installs a listener to receive notification when the text of any
     * {@code JTextComponent} is changed. Internally, it installs a
     * {@link DocumentListener} on the text component's {@link Document},
     * and a {@link PropertyChangeListener} on the text component to detect
     * if the {@code Document} itself is replaced.
     *
     * Taken from
     *
     * @param text any text component, such as a {@link JTextField}
     *        or {@link JTextArea}
     * @param changeListener a listener to receieve {@link ChangeEvent}s
     *        when the text is changed; the source object for the events
     *        will be the text component
     * @throws NullPointerException if either parameter is null
     */
    public static void addChangeListener(JTextComponent text, ChangeListener changeListener) {
        Objects.requireNonNull(text);
        Objects.requireNonNull(changeListener);
        DocumentListener dl = new DocumentListener() {
            private int lastChange = 0, lastNotifiedChange = 0;

            @Override
            public void insertUpdate(DocumentEvent e) {
                changedUpdate(e);
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                changedUpdate(e);
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                lastChange++;
                SwingUtilities.invokeLater(() -> {
                    if (lastNotifiedChange != lastChange) {
                        lastNotifiedChange = lastChange;
                        changeListener.stateChanged(new ChangeEvent(text));
                    }
                });
            }
        };
        text.addPropertyChangeListener("document", (PropertyChangeEvent e) -> {
            Document d1 = (Document) e.getOldValue();
            Document d2 = (Document) e.getNewValue();
            if (d1 != null)
                d1.removeDocumentListener(dl);
            if (d2 != null)
                d2.addDocumentListener(dl);
            dl.changedUpdate(null);
        });
        Document d = text.getDocument();
        if (d != null)
            d.addDocumentListener(dl);
    }
}

Related

  1. addJTextComponentListeners(JTextComponent c, Object... objs)
  2. addTextListener( JTextComponent c, Runnable listener )
  3. addTextUpdateListener(JTextComponent textComponent, Consumer listener)
  4. addUndoRedo(JTextComponent comp)