2

My situation: I have a JTextPane with its own syntax highlighting. I have it set so that when the user stops typing, it updates the style in the text using the setCharacterAttributes() method.

My Issue: When these updates to the style are not performed, the undo manager works as expected. But when I do use it, the undo manager counts those style changes as actual undo-able actions! Meaning hitting Ctrl+z (I have it bound to undo when pressed) it just un-colors the last character i typed. Rather than actually removing/undoing it.

How would I get it so undo-ing and redo-ing only affects text changes and not style/font changes in my StyledDocument?

Thank you.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373

1 Answers1

1

It sounds like you need to make use of addEdit or the Significant attribute as explained by the UndoManager:

The UndoManager makes use of isSignificant to determine how many edits should be undone or redone. The UndoManager will undo or redo all insignificant edits (isSignificant returns false) between the current edit and the last or next significant edit. addEdit and replaceEdit can be used to treat multiple edits as a single edit, returning false from isSignificant allows for treating can be used to have many smaller edits undone or redone at once. Similar functionality can also be done using the addEdit method.

Sources: https://docs.oracle.com/javase/8/docs/api/javax/swing/undo/UndoableEdit.html

sorifiend
  • 5,927
  • 1
  • 28
  • 45
  • 1
    I tried this and ran into the issue that I could only undo once, and not undo. Rather than fixing the problem I just created my own custom undo manager class, which works pretty well. Thank you for the reply. – Antonio Ferreras Feb 05 '19 at 23:22