Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to do undo/redo in JTextArea.

  1. #1
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default 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)
    {
       int start=ta.getSelectionStart();
        String startText=ta.getText().substring(0, start);
        String endText=ta.getText().substring(start);
        String data = (String) Toolkit.getDefaultToolkit()
                    .getSystemClipboard().getData(DataFlavor.stringFlavor); 
        String res=startText+data+endText;
        ta.setText(res);
     
     
    }
     
     
    }


    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?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to do undo/redo in JTextArea.

    Have you seen this?

  3. #3
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: How to do undo/redo in JTextArea.

    Quote Originally Posted by GregBrannon View Post
    Have you seen this?
    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.

    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.)

Similar Threads

  1. Issues with undo functions
    By JavaInProgress in forum Android Development
    Replies: 3
    Last Post: June 10th, 2013, 05:20 PM
  2. Help implementing an undo function
    By JaeDuck in forum Object Oriented Programming
    Replies: 2
    Last Post: February 18th, 2013, 07:31 AM
  3. Problem implementing an undo function.
    By JaeDuck in forum Object Oriented Programming
    Replies: 0
    Last Post: February 17th, 2013, 09:15 PM
  4. undo delete method
    By Trunk Monkeey in forum Object Oriented Programming
    Replies: 7
    Last Post: February 7th, 2012, 03:49 PM
  5. [SOLVED] how do I undo an AlphaComposite?
    By gib65 in forum AWT / Java Swing
    Replies: 3
    Last Post: October 21st, 2010, 04:23 PM