I have a text editor I've been working on, and I'm having some trouble with the undo/redo functions... It doesn't work, and I don't know what's wrong with it. I posted all of my code dealing with the undo function. I know it's a lot, but maybe you wont mind helping me. Any suggestions will help.

UndoManager undoManager;
textArea2.getDocument().addUndoableEditListener(this);
	textArea2.addFocusListener(this);
public void createUndoManager()
 {
	 undoManager = new UndoManager();
 }

 public void removeUndoManager()
 {
	 undoManager.end();
 }
  public void focusGained(FocusEvent fe)
 {
	 createUndoManager();
 }
 public void focusLost(FocusEvent fe)
 {
	 removeUndoManager();
 }
 public void undoableEditHappened(UndoableEditEvent uee) 
 {
	 undoManager.addEdit(uee.getEdit());
 }
public void undoableEditHappened(UndoableEditEvent uee) 
 {
	 undoManager.addEdit(uee.getEdit());
 }
if ((ae.getSource() == btnUndo) || ae.getActionCommand().equals("Undo"))
{	
 try
	 {
		 undoManager.undo();
	 }
	 catch (CannotUndoException cue)
	 {
		 Toolkit.getDefaultToolkit().beep();
	 }
 }
}

Hi everyone,

You are resetting your UndoManager class every time so there is always nothing to undo. Basically remove the createUndoManager() function and create that class once as a global class with a global instance only once.

Don't forget the import stsement for the undo class as well

Richard West

This question has already been answered. Start a new discussion instead.