Adding undo and redo to a Text Component

zhaozj2021-02-17  27

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

转载请注明原文地址:https://www.9cbs.com/read-28637.html

New Post(0)