Java JTextComponent addTextListener( JTextComponent c, Runnable listener )

Here you can find the source of addTextListener( JTextComponent c, Runnable listener )

Description

add Text Listener

License

Open Source License

Declaration

public static void addTextListener( JTextComponent c, Runnable listener )

Method Source Code

    //package com.java2s;

    import javax.swing.event.DocumentEvent;
    import javax.swing.event.DocumentListener;
    import javax.swing.text.Document;
    import javax.swing.text.JTextComponent;

    public class Main {
        public static void addTextListener( JTextComponent c, Runnable listener )
{
    DocumentListener docListener = new DocumentListener( )
    {//from   w  ww. j a  v a  2s . c om
        public void insertUpdate( DocumentEvent ev ) { listener.run( ); }
        public void removeUpdate( DocumentEvent ev ) { listener.run( ); }
        public void changedUpdate( DocumentEvent ev ) { listener.run( ); }
    };

    c.addPropertyChangeListener( "document", ( ev ) ->
    {
        Document oldDoc = ( Document ) ev.getOldValue( );
        if ( oldDoc != null )
        {
            oldDoc.removeDocumentListener( docListener );
        }

        Document newDoc = ( Document ) ev.getNewValue( );
        if ( newDoc != null )
        {
            newDoc.addDocumentListener( docListener );
        }

        listener.run( );
    } );

    Document doc = c.getDocument( );
    if ( doc != null )
    {
        doc.addDocumentListener( docListener );
    }
}
    }

Related

  1. addChangeListener(JTextComponent text, ChangeListener changeListener)
  2. addJTextComponentListeners(JTextComponent c, Object... objs)
  3. addTextUpdateListener(JTextComponent textComponent, Consumer listener)
  4. addUndoRedo(JTextComponent comp)
  5. adjustForLineComment(JTextComponent editor, int iStart)
  6. appendToText(JTextComponent textComp, String s)