Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to implement an "undo" function in textarea?

Posted on 1997-05-05
8
Medium Priority
?
454 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 80 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

688 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