Search code examples
javajtextpaneundo-redo

Undo And Redo In JTextPane Ignoring Style Changes


I was wondering if there is a way to ignore the text style changes in a JTextPane while using Swing's UndoManager?


Solution

  • I've never tried it, but I would guess you can create a custom UndoManager.

    You would need to override the undoableEditHappend(...) method to ignore an attribute change:

    @Override
    public void undoableEditHappened(UndoableEditEvent e)
    {
        //  Check for an attribute change
    
        AbstractDocument.DefaultDocumentEvent event =
            (AbstractDocument.DefaultDocumentEvent)e.getEdit();
    
        if  (event.getType().equals(DocumentEvent.EventType.CHANGE))
            return
        else
            super.undoableEditHappened(e);
    }