This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to implment Undo/Redo for a editor

 
Dinesh Bang
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

I am looking to implement the undo/redo functionality for my custom editor. Any idea's how to achieve that ?
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each JTextComponent (including JTextArea and JTextField) have a Document. To this Document you can attach a UndoableEditListener. Combine this with an UndoManager.
 
Dinesh Bang
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2580
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the secton from the Swing tutorial on "Text Component Features" for a working example.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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():
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic