Link to home
Start Free TrialLog in
Avatar of hx
hx

asked on

How to implement an "undo" function in textarea?

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?
ASKER CERTIFIED SOLUTION
Avatar of mlimotte
mlimotte

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hx
hx

ASKER

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?
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.

Avatar of hx

ASKER

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?
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.





Avatar of hx

ASKER

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?
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?

Avatar of hx

ASKER

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 ! :-)