Search code examples
javaswingevent-handlingdocumentjeditorpane

JEditorPane - In what order are edit events called?


Let's say you have a JEditorPane with which you have registered both an UndoableEditListener and a DocumentListener. When you change the underlying document's contents by typing in the editor pane, what is the order in which the events are fired?

In addition, I assume that, in the interest of efficiency, calling either of these methods:

  • UndoableEdit.undo()
  • UndoableEdit.redo()

...will not trigger a new UndoableEdit event, but will still trigger a new DocumentEvent. Can anyone confirm whether this is indeed the case?


Solution

  • After trying what camickr suggested, it seems that this is the order of events:

    1. When typing in the editor pane (including cutting, copying and pasting) or changing the document font/style, the document will fire a DocumentEvent before it fires an UndoableEditEvent.

    2. When calling UndoableEdit.undo() or UndoableEdit.redo(), only a corresponding DocumentEvent will be fired.