Store your states on a stack, linked list or similar. Undo means go back one place on the stack. Redo means go one place forwards. Back from state 0 means throw CantUndoException. Forward from state n means throw CantRedoException. Any changes of state mean lost whatever is ahead of the present point on the stack, probably easiest done by resetting a maxIndex variable.
The alternative is to store each state change as an operation and put the operations into a list. You can the insert an operation in the middle of the list if you so wish.
JTextComponent, the base class for most text controls in Swing, is your friend here.
Each JTextComponent has a Document that can be retrieved using the "getDocument" method. Document has a method "addUndoableEditListener" which allows you to attach UndoableEditListeners. These trigger "undoableEditHappened" events; the event has an UndoableEdit.
Create an instance of UndoManager, and each time such an UndoableEdit occurs add it with the "addEdit" method.
Now this will register and store your undo actions, but there's still no means of executing them. You can do this through a menu, popup menu, key presses or a combination.
The following code shows part of a key listener that will undo or redo, using CTRL+Z for undo and CTRL+Y for redo:
Now you can investigate UndoManager for some more cool methods, which include "getUndoPresentationName", "getRedoPresentationName" and "getUndoOrRedoPresentationName".
I've also been using Java's own undo mechanism for a few months now, and writing support for text components was of course the first to try it on
That one's easy as the document can listen for undoable edits on its own. For other controls (such as JSpinner and JComboBox, I've got those working too) you have to write custom undoable edit classes. Sure, you can use the text document used in JSpinner, but there's one drawback: setText first removes the entire text (one edit), then adds the new text (two edits). So I found out that undoing to the previous value would require two undo actions each time. In the end, I listen to the stateChanged event of the JSpinner, and create a custom undoable edit there based on the old and new values. That class' undo() method sets the old value, the redo() methods sets the new value. Quite easy in fact.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com