manages a list of
, providing a way to undo or redo the appropriate edits. There are two ways to add edits to an
. Add the edit directly using the
method, or add the
to a bean that supports
. The following examples creates an
and adds it as an
UndoManager undoManager = new UndoManager();
JTextField tf = ...;
UndoManager maintains an ordered list of edits and the index of the next edit in that list. The index of the next edit is either the size of the current list of edits, or if
undo has been invoked it corresponds to the index of the last significant edit that was undone. When
undo is invoked all edits from the index of the next edit to the last significant edit are undone, in reverse order. For example, consider an
UndoManager consisting of the following edits: AbcD. Edits with a upper-case letter in bold are significant, those in lower-case and italicized are insignificant.
As shown in figure 1 , if D was just added, the index of the next edit will be 4. Invoking
undo results in invoking
undo on D and setting the index of the next edit to 3 (edit c), as shown in the following figure.
The last significant edit is A, so that invoking
undo again invokes
undo on c, b, and A, in that order, setting the index of the next edit to 0, as shown in the following figure.
redo results in invoking
redo on all edits between the index of the next edit and the next significant edit (or the end of the list). Continuing with the previous example if
redo were invoked,
redo would in turn be invoked on A, b and c. In addition the index of the next edit is set to 3 (as shown in figure 2 ).
Adding an edit to an
UndoManager results in removing all edits from the index of the next edit to the end of the list. Continuing with the previous example, if a new edit, e, is added the edit D is removed from the list (after having
die invoked on it). If c is not incorporated by the next edit (
c.addEdit(e) returns true), or replaced by it (
e.replaceEdit(c) returns true), the new edit is added after c, as shown in the following figure.
end has been invoked on an
UndoManager the superclass behavior is used for all
UndoableEdit methods. Refer to
CompoundEdit for more details on its behavior.
Unlike the rest of Swing, this class is thread safe.
Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans™ has been added to the
java.beans package. Please see