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

I finally got it fixed about a week ago..I was making it way more difficult than it was.

Please Sand me your all code of the editor.
Plz
Plz
Thanks

do your own homework kid.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.