How to do undo/redo in JTextArea.
I want to do undo/redo with a JTextArea. I have found some stuff on this already but as I have a JPopupMenu that does some of the actions of the JMenuItems that also do them, and, as the JMenuItems in the JPopupMenu also apply polymorphically to other classes that aren't using undo/redo, the code can't be be put in my class that applies this JPopupMenu to those JTextComponents.
I went here. However, I'm not quite sure of how the mechanisms of this would work, especially in my case. (I think though, that, in the case of the JTextArea and the popup menu that goes with it, I can add an action listener to them via the route of getComponentPopupMenu() )
Add an undo/redo function to your Java apps with Swing - JavaWorld
How would I go about this? I still am a bit confused on how to do this and what I need to ensure that I can apply this to functions that I haven't written yet but will later, or how to do it with a replaceAll() where multiple replaces may be done at once.
Also, is it possible to have an UndoManager support multiple undos or redos? (i.e., can I make a Notepad that allows more than one undo/redo or can I, unless I make all the code myself, only be stuck with one undo/redo like the Windows Notepad does?)
I'm trying to make a PasteAction that can be undone. I don't know what to put as I can't guarantee, given multiple undos possible, that the user might not have, say, added text before the point where the paste stuff was added, hence making storing the spot where it was originally added dubious as it may not reference the actual spot anymore?
I know that for cut, I can add it back, but I'd need to know where it started and stopped the cut so that I can put it back, but, again, with multiple edits possible, it could be changed to no longer put it back in the proper place.
Also, I'm looking online and I was wondering also, can undo manager make sure that it undoes all the keys typed between a pause or the last undoable action that was added? I mean, if I typed "Undoable" in a JTextArea and then told it to undo, would it remove the whole word or sequence or words or just remove the last letter, say, making it say "Undoabl" and making the user have to undo each letter one by one (a great way to turn people off of your product!!!!)?
Preferably, I'd like to somehow make it to use the CompoundEdit class or whatever it was called to somehow make that that a single edit made of several smaller ones (the characters).
I can make a paste action that behaves like a JTextArea (JTextComponent to be more exact, but you get the point) paste action.
public class PasteAction extends AbstractAction
public void actionPerformed(ActionEvent e)
String startText=ta.getText().substring(0, start);
String data = (String) Toolkit.getDefaultToolkit()
I do get that if I undid that, how, I don't know yet, then if I did some other action, that the redo queue might be emptied as I did something that could be undone. (Or it's something like that where it would make it so that it couldn't redo.) Assuming that I could redo the paste action after undoing it, how would I?
Am I going to need DocumentListeners to keep track to see if an action that could be undo/redone happens? (I have a feeling I probably do but thought I'd ask.)
For starters, how would I support an undo/redo of a paste action and how could I use UndoManager (or something else in java that doesn't require a third party jar, which I have trouble getting into JGrasp as I keep needing to look up how to import them) or is there nothing to support being able to store more than one undo/redo at a time?
Re: How to do undo/redo in JTextArea.
Re: How to do undo/redo in JTextArea.
Yes. Those examples are too vague to figure out how to do even a single Action and Edit pair to undo/redo. At least, for a JTextArea. I've seen one with a JList, and undoing/redoing adding/removing elements from it.
Originally Posted by GregBrannon
However, I just can't figure out how to do this.
Do I even need to make my own UndoableEdit and AbstractAction classes for this or will the Document and stuff be able to do it all for me (I can't find any good examples and all the ones I can find pretty much are just using UndoManager and calling undo() and redo(). If there's something more, I can't find it yet.)
Also, I thought that UndoManager would undo every stinking character I typed on an insertUpdate() so that it would really tick the user off to have to undo them one character at a time.
I don't want that. I want it to undo/redo the entire thing, not just one character at a time.
I think there was a way I could extend CompoundEdit to do that.
Still, I'm pretty lost here.
However, does swing and the UndoableEditListener and DocumentListener add most of the edits to keep track of for me?
I need to find a way to make it use CompoundEdit to undo deletes in full, and not just one character at a time, and the same thing for insertion.
Also, I need such a thing if I do a replaceAll() function (I know String has one, but I want it to count how many times it changes it, like it does in Microsoft Notepad, thus I can't really use the replaceAll() of the String class, though I can use the replace() method of the String class and call it multiple times.)