ChangeEvt

JTextComponent textcomp = new JTextPane();
textcomp.setText("Initial Text");
textcomp.getDocument().addDocumentListener(new DocumentListener() {
    // This method is called after an insert into the document
    public void insertUpdate(DocumentEvent evt) {
        // Get index of newly inserted characters
        int off = evt.getOffset();

        // Get length of new inserted characters
        int len = evt.getLength();

        try {
            // Get inserted string
            String str = evt.getDocument().getText(off, len);
        } catch (BadLocationException e) {
        }
    }

    // This method is called after a removal from the document
    public void removeUpdate(DocumentEvent evt) {
        // Get starting index of removed characters
        int off = evt.getOffset();

        // Get length of removed characters
        int len = evt.getLength();

        // The removed characters are not available
    }

    // This method is called after one or more attributes have changed.
    // This method is not called when characters are inserted with attributes.
    public void changedUpdate(DocumentEvent evt) {
        // Get starting index of characters whose attributes have changed
        int off = evt.getOffset();

        // Get length of characters whose attributes have changed
        int len = evt.getLength();
    }
});

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.