I was wondering if there is a way to ignore the text style changes in a JTextPane while using Swing's UndoManager?
Undo And Redo In JTextPane Ignoring Style Changes
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);
}
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.