Here you can find the source of addTextListener( JTextComponent c, Runnable listener )
public static void addTextListener( JTextComponent c, Runnable listener )
//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 ); } } }