Undo And Redo In JTextPane Ignoring Style Changes
Asked Answered
P

1

6

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

Picrate answered 6/1, 2016 at 22:46 Comment(0)
P
5

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);
}
Paleobotany answered 6/1, 2016 at 23:5 Comment(3)
Would you happen to know how to get the text that was added/removed?Picrate
No I have tried many times but have never been able to get that information.Paleobotany
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!Stockinet

© 2022 - 2024 — McMap. All rights reserved.