Solved

How to implement an "undo" function in textarea?

Posted on 1997-05-05
8
447 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

740 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