Undo and redo are two essential functions for text editors. To implement this function, use the undomanager class in the javax. Swing. Undo package.
The undomanager class has the following common methods:
Boolean canundo () -- determines whether the Undo operation can be performed. If yes, true is returned.
Boolean canredo () -- determines whether a restoration operation can be performed. If yes, true is returned.
Void undo () -- undo the operation.
Void Redo () -- Restore.
However, you must register a listener for the Undo and recovered objects before you perform the Undo and recovery operations. In the text editor, an object is text. For example, declare two objects first:
Jtextarea text = new jtextarea ();
Undomanager undomg = new undomanager ();
Next, add a listener for text:
Text. getdocument (). addundoableeditlistener (undomg );
The reason for writing getdocument () is that the object for registering the listener is text, not a text box. Addundoableeditlistener (undomg) registers a listener for text. The prototype of this method is:
Void addundoableeditlistener (undoableeditlistener listener)
It can be seen that the parameter of this method should be a parameter of the undoableeditlistener interface type, and the parameter we use is of the undomanager type, because the undomanager class has implemented the undoableeditlistener interface.
Now you can perform the Undo and recovery operations. The method is:
Undomg. Undo (); // undo
Undomg. Redo (); // restore
But this is not perfect, because if the two statements cannot be undone or restored, they will not play any role. Therefore, we use the following method:
If (undomg. canundo () {// undo
Undomg. Undo ();
}
If (undomg. canredo () {// restore
Undomg. Redo ();
}
In this way, the Undo and restore functions of the text editor are easily implemented. The following is an example:
Import Java. AWT. *; <br/> Import Java. AWT. event. *; <br/> Import javax. swing. *; <br/> Import javax. swing. undo. *; </P> <p> class undodemo extends jframe {</P> <p> static jtextarea text = new jtextarea (); <br/> static jpanel pnl = new jpanel (); <br/> static jbutton unbtn = new jbutton ("undo "); <br/> static jbutton rebtn = new jbutton ("Restore"); <br/> static undomanager undomg = new undomanager (); </P> <p> undodemo () {</P> <p> super ("undo and restore function instance"); <br/> setvisible (true); <br/> setsize (400,300 ); <br/> setlocationrelativeto (null); <br/> setdefaclocloseoperation (jframe. exit_on_close); <br/> setlayout (New borderlayout (5, 5); </P> <p> pnl. setlayout (New flowlayout (5); <br/> pnl. add (unbtn); <br/> pnl. add (rebtn); <br/> Add (pnl, borderlayout. north); <br/> Add (text, borderlayout. center); </P> <p> text. getdocument (). addundoableeditlistener (undomg); </P> <p> unbtn. addactionlistener (New actionlistener () {<br/> Public void actionreceivmed (actionevent EVT) {<br/> If (undomg. canundo () {<br/> undomg. undo (); <br/>}else {<br/> joptionpane. showmessagedialog (null, "unrecoverable", "warning", joptionpane. warning_message); <br/>}< br/>}); </P> <p> rebtn. addactionlistener (New actionlistener () {<br/> Public void actionreceivmed (actionevent EVT) {<br/> If (undomg. canredo () {<br/> undomg. redo (); <br/>}else {<br/> joptionpane. showmessagedialog (null, "cannot be restored", "warning", joptionpane. warning_message); <br/>}< br/> }); <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> New undodemo (); <br/>}< br/>}
The running effect is as follows: