5

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

1 Answers1

4

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);
}
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Would you happen to know how to get the text that was added/removed? – ThePrimedTNT Jan 08 '16 at 05:22
  • No I have tried many times but have never been able to get that information. – camickr Jan 08 '16 at 15:32
  • I'm performing some simple syntax higlighting in a JTextPane and the lack of built-in Undo/Redo was annoying. Fortunately I stumbled across the UndoManager, but unfortunately the syntax highlighting produced numerous insignificant edits that would have made the UndoManager useless without this great answer. Thanks! – drwatsoncode Mar 08 '17 at 21:32