Java application skills-Implementation of text revocation and restoration Functions

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.