• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Undo and Redo in JText viewer?

 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
can anyone give me any idea how to do Undo and Redo in JText viewer? I am completely lost..!
 
Marshal
Posts: 75869
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Store your states on a stack, linked list or similar.
Undo means go back one place on the stack.
Redo means go one place forwards.
Back from state 0 means throw CantUndoException.
Forward from state n means throw CantRedoException.
Any changes of state mean lost whatever is ahead of the present point on the stack, probably easiest done by resetting a maxIndex variable.

The alternative is to store each state change as an operation and put the operations into a list. You can the insert an operation in the middle of the list if you so wish.
 
Sheriff
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JTextComponent, the base class for most text controls in Swing, is your friend here.

Each JTextComponent has a Document that can be retrieved using the "getDocument" method. Document has a method "addUndoableEditListener" which allows you to attach UndoableEditListeners. These trigger "undoableEditHappened" events; the event has an UndoableEdit.

Create an instance of UndoManager, and each time such an UndoableEdit occurs add it with the "addEdit" method.

Now this will register and store your undo actions, but there's still no means of executing them. You can do this through a menu, popup menu, key presses or a combination.

The following code shows part of a key listener that will undo or redo, using CTRL+Z for undo and CTRL+Y for redo:

Now you can investigate UndoManager for some more cool methods, which include "getUndoPresentationName", "getRedoPresentationName" and "getUndoOrRedoPresentationName".
 
Campbell Ritchie
Marshal
Posts: 75869
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brilliant, Rob. I never realised that.
 
Rob Spoor
Sheriff
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've also been using Java's own undo mechanism for a few months now, and writing support for text components was of course the first to try it on

That one's easy as the document can listen for undoable edits on its own. For other controls (such as JSpinner and JComboBox, I've got those working too) you have to write custom undoable edit classes.
Sure, you can use the text document used in JSpinner, but there's one drawback: setText first removes the entire text (one edit), then adds the new text (two edits). So I found out that undoing to the previous value would require two undo actions each time. In the end, I listen to the stateChanged event of the JSpinner, and create a custom undoable edit there based on the old and new values. That class' undo() method sets the old value, the redo() methods sets the new value. Quite easy in fact.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic