It looks like you need to use DocumentListener to track changes. Events in the document listener will tell you how many characters are added / removed in any given change, and also gives a link to the Document , which supports the text area.
The following is an example implementation of a document listener for a JTextArea called textArea :
textArea.getDocument().addDocumentListener( new DocumentListener() { public void changedUpdate( DocumentEvent e ) { } public void insertUpdate( DocumentEvent e ) { System.out.println( "insertUpdate: Added " + e.getLength() + " characters, document length = " + e.getDocument().getLength() ); } public void removeUpdate( DocumentEvent e ) { System.out.println( "removeUpdate: Removed " + e.getLength() + " characters, document length = " + e.getDocument().getLength() ); } });
This listener will detect cutouts and pastes, as well as keystrokes.
source share