Solved

How to implement an "undo" function in textarea?

Posted on 1997-05-05
8
442 Views
Last Modified: 2006-11-17
It is very common that GUI applications have some menus such
as "File" and "Edit". And usually,"Edit" menu includes such
menu items as "Undo","Cut","Copy","Paste" and so on.
 
I want to write a GUI application using Java 1.0.2. However,
I don't know clearly how to implement the edit menu,esp. how
to implement that undo function. Making a copy of the edited
text or what?
0
Comment
Question by:hx
  • 4
  • 4
8 Comments
 
LVL 1

Accepted Solution

by:
mlimotte earned 40 total points
ID: 1220101
Override the gotFocus() method to save the current text (the unedited text) in a String.  When undo is selected, just use setText() to put the saved String back in place.


0
 

Author Comment

by:hx
ID: 1220102
This seems like undoing all the modification since last saving of the file. Is there a possibility to save the text just before the last action which the user took?
0
 
LVL 1

Expert Comment

by:mlimotte
ID: 1220103
Well, it depends on what you define as the last action.  

The gotFocus() method will be fired when the user clicks (or otherwise focuses) in the TextArea.  As soon as they click on another component, there is the possibility of again receiving a new gotFocus event (i.e. the start of a new action).

However, perhaps you are implying that the TextArea is the only "main" component in the app (e.g. a text editor application).  If this is the case, then the got focus method would not be to interesting.  You will need to define what you mean by "an action".  Some editors consider each key press to be an action and provide multiple levels of undo, in this case perhaps the key press event would be helpful.  

I think you need to clarify your question.

0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 

Author Comment

by:hx
ID: 1220104
Thanks for concerning my question.

Yes,the main component of my app is the textarea. According to undo,I want to implement the following:

a.After the user keeps on inputting some text,he issues the undo command.Then undo all the inputting.In this case,the gotFocus() method is just fine.

b.After the user keeps on inputting a long paragraph of text,he deletes a sentence by mistake. Then he wants to undo the deletion.

So my question is how to get known when the user takes the delete action and save the text just before the last continuous deletion? I think when I catch the "Delete" key press event,one character has been deleted. So it seems that I can't save completely the text before deletion. What can I do about it?
0
 
LVL 1

Expert Comment

by:mlimotte
ID: 1220105
Catch the delete key press:

define a flag variable, such as
   Boolean deleteSequence = false;

When delete is first hit, test the flag.

If deleteSequence is false then save the current text value, and set deleteSequence to true.

If deleteSequence is true  do nothing.

If any other key is caught, set deleteSequence to false.

Now, if the user presses delete 20 times, only the first press will cause the text to be stored.  So if an undo command comes along, you can restore the text from before the first delete.





0
 

Author Comment

by:hx
ID: 1220106
Unfortunately,I failed to catch the delete key press in TextArea.
It seems that only GOT_FOCUS and LOST_FOCUS events can be caught within TextArea. So if I want to catch keyboard actions, I have to extends Canvas and implement a textarea class manually.

Any comment or I miss something?
0
 
LVL 1

Expert Comment

by:mlimotte
ID: 1220107
Sure, you can catch the delete key in a TextArea (anything that's subclassing Component, for that matter).  Just override the keyDown() method and look for (event.key == 127).

Have you tried it?  Are you having a specific problem?

0
 

Author Comment

by:hx
ID: 1220108
I tried in JDK 1.0.2,but nothing happened when I pressed any key in TextArea. Today,I recompile my program with Java compiler V1.1,and run it in JDK1.1 with everything ok except the value of delete key is 65535.

Anyway,thank you very much for all your help ! :-)
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question