THESE CODE EXAMPLES AND OTHER MATERIALS Are Subject To Sun Microsystems, Inc. Legal Terms
Adding undo and redo to a Text Component
Jtextarea textarea = new jtextarea ();
Final Undomanager undo = new undomanager ();
Document doc = textarea.getDocument ();
// The next two lines shouth be in one line.
Doc.addundoableEditListener
New undoableEditListener () {
The next two las..
Public void undoableEdithappend (
UndoableEdITEVENT EVT) {
undo.addedit (evt.getedit ());
}
});
Textarea.getActionMap (). PUT ("undo",
New AbstractAction ("undo") {
Public void actionperformed (ActionEvent EVT) {
Try {
IF (undo.canundo ()) {
undo.undo ();
}
} catch (cannotundoExcection e) {
}
}
});
Textarea.getinputMap (). put (Keystroke.getKeystroke)
"Control Z"), "Undo");
Textarea.getActionMap (). Put ("redo",
New AbstractAction ("redo") {
Public void actionperformed (ActionEvent EVT) {
Try {
IF (undo.canredo ()) {
UNDO.REDO ();
}
} catch (cannotredoExcection e) {
}
}
});
Textarea.getinputMap (). put (Keystroke.getKeystroke)
"Control Y", "Redo");
Examplets TM Provided by Permission of The Publisher, Adise-Wesley, And Author Patrick Chan.order this book from Amazon