Results 1 to 7 of 7
  1. #1
    Spirit356 is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default Trying to create an undo function using an arraylist.

    Hello everyone first time poster, just looking for some help to make sure I'm doing this in the right way. So down to business. I'm trying to create an undo function for an image program using an arraylist which stores the image. Now the normal command for saving the image is this: "imagePanel.setImage(currentImage)"

    CurrentImage is the name of the class which I have created the arraylist was. Now before and after everytime a change is made to the image I save the image into the arraylist using this command: "images.add(currentImage)" images is the name of the arraylist so my undo method then has the following: "imagePanel.setImage(images.get(images.size()-1)"

    This however doesn't seem to work at all and it's beginning to get just a little frustrating so I was wondering if I'm on the right track at all before I froth at the mouth. Thanks in advance for any help!

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    i think you're on the right track. One problem is that for your undo, you always take the last image:

    imagePanel.setImage(images.get(images.size()-1)

    the next undo will not go to previous image, but will return the same one. Perhaps you should return the last image AND delete it from the ArrayList so that next time around images.size() is 1 smaller and you get the next image in line.

  3. #3
    Spirit356 is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    Hey thanks for the help, I tried what you said but I still can't get past the problem it seems. The image won't change it all it simply says in the same state when I try undo and I have no idea what the problem is.

  4. #4
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    could it be that when you execute undo, this happens:

    - you remove the last element from array list
    - you call "set image", which executes "images.add(currentImage)", and you're right back where you started?

  5. #5
    Spirit356 is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    Sorry, neither of those seem to work. I still have no idea what the problem is and there are no error messages to help lol. This seems like an unexplained mystery.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Spirit356 View Post
    Sorry, neither of those seem to work. I still have no idea what the problem is and there are no error messages to help lol. This seems like an unexplained mystery.
    Maybe you can use an UndoManager, it manages UndoableEdit objects. The latter is an interface (so you're free to implement it the way you want), the former manages a bunch of implementations of UndoableEdits. Read the API documentation for the two.

    kind regards,

    Jos

  7. #7
    Spirit356 is offline Member
    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Default

    Sorry if I sound a bit silly here still after the help but I tried using the UndoManager but now I seem to be getting a CannotUndoException error? Any idea on how to fix this?

    Edit -Actually if anyone could show the proper use the the manager and interface I think that would help.
    Last edited by Spirit356; 04-27-2010 at 07:32 PM.

Similar Threads

  1. Replies: 3
    Last Post: 12-04-2009, 03:08 PM
  2. Search function for ArrayList?
    By javanoobie in forum New To Java
    Replies: 11
    Last Post: 04-17-2009, 08:38 PM
  3. How to create Cookie for login function
    By javanewbie in forum Advanced Java
    Replies: 0
    Last Post: 11-11-2008, 04:03 PM
  4. SWT Undo Redo
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:43 PM
  5. undo java
    By new214 in forum New To Java
    Replies: 1
    Last Post: 11-20-2007, 09:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •