This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes How to implment Undo/Redo for a editor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to implment Undo/Redo for a editor" Watch "How to implment Undo/Redo for a editor" New topic
Author

How to implment Undo/Redo for a editor

Dinesh Bang
Ranch Hand

Joined: Jun 15, 2006
Posts: 52
Hi Ranchers,

I am looking to implement the undo/redo functionality for my custom editor. Any idea's how to achieve that ?


SCJP 1.4,SCBCD 1.3, SCWCD 1.5, ???
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19908
    
  23

Each JTextComponent (including JTextArea and JTextField) have a Document. To this Document you can attach a UndoableEditListener. Combine this with an UndoManager.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Dinesh Bang
Ranch Hand

Joined: Jun 15, 2006
Posts: 52
Thanks for quick response

Just to add up, Is there a generic way to handle Undo/Redo for whole perspective.
In my application I will be required to have it on all the views, project explorer ,editors etc.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2261
    
    8
Read the secton from the Swing tutorial on "Text Component Features" for a working example.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19908
    
  23

Dinesh Bang wrote:Just to add up, Is there a generic way to handle Undo/Redo for whole perspective.
In my application I will be required to have it on all the views, project explorer ,editors etc.

You should still use UndoableEdit and UndoManager, but you must handle your own events that will create an UndoableEdit. Hint: extend AbstractUndoableEdit and override undo() and redo():
 
Consider Paul's rocket mass heater.
 
subject: How to implment Undo/Redo for a editor