Solved

How to implement an "undo" function in textarea?

Posted on 1997-05-05
8
441 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now