The UndoManager Class
UndoManager
is an extension of CompoundEdit
that can track a history of
edits, allowing them to be undone or redone one at time. Additionally,
it implements UndoableEditListener
by
calling addEdit( )
each time an
UndoableEditEvent
is fired. This
allows a single UndoManager
to be
added as a listener to many components that support undo, providing a
single place to track all edits and populate an undo menu for the entire
application.
It may seem a bit strange that UndoManager
extends CompoundEdit
. We’ll explain why shortly, but
first it’s important to understand the primary ways in which UndoManager
acts differently than CompoundEdit
. For starters, when you add an
edit to an UndoManager
, it is placed
in a list of edits available for undo. When you call undo( )
, only the first (significant) edit is
undone. This is different from the behavior of CompoundEdit
, in which a call to undo( )
results in a call to undo( )
on all of the added edits.
Another major difference between UndoManager
and its superclass is the
semantics of the inProgress
property. In CompoundEdit
, we could add new edits only when
we were inProgress, and only after calling end( )
could undo(
)
or redo( )
be called. In
contrast, UndoManager
allows undo( )
and redo(
)
to be called while it is inProgress.
Furthermore, when end( )
is called,
it stops supporting sequential undo/redo behavior and starts acting like
CompoundEdit
(undo( )
and redo(
)
call their superclass implementations when the UndoManager ...
Get Java Swing, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.