Hi all,
 
I have to do undo/redo operation in JTextField.  I was able to do with UndoManager, but the problem when using UndoManager is when we enter some text it undoes by each character rather the whole world entered by us.  I have used CompoundEdit  for this purporse and attached a undoableEditListener to the document of JTextField. but this does't work as expected giving eratic behaviour(unconsistent undo operations).  Please suggest me a better alternative or correction for doing undo/redo operation in JTextField which undo whatever the user entered in a single go like in NotePad editor.
 
I am attaching my code for the reference  Please let me know where i am missing something
 
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.undo.*;
import javax.swing.event.*;
 
public class UndoRedoTextApp extends JFrame
{
  protected JTextArea m_editor = new JTextArea();
  protected UndoManager undoManager = new UndoManager();
  protected CompoundEdit compoundEdit; // = new CompoundEdit();
  protected JButton m_undoButton = new JButton("Undo");
  protected JButton m_redoButton = new JButton("Redo");
 
  public UndoRedoTextApp() {
    super("Undo/Redo Demo");
 
    JPanel buttonPanel = new JPanel(new GridLayout());
    buttonPanel.add(m_undoButton);
    buttonPanel.add(m_redoButton);
 
    JScrollPane scroller = new JScrollPane(m_editor);
 
    getContentPane().add(buttonPanel, BorderLayout.NORTH);
    getContentPane().add(scroller, BorderLayout.CENTER);
 
    m_editor.getDocument().addUndoableEditListener(new UndoableEditListener() {
      public void undoableEditHappened(UndoableEditEvent e) {
          if( compoundEdit == null || compoundEdit.isInProgress() == false)
          {
              compoundEdit = new CompoundEdit();
              System.out.println("new edit is created");
          }
            boolean added = compoundEdit.addEdit(e.getEdit());
            updateButtons("undoableedit");
      }
    });
 
    m_undoButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
                   compoundEdit.end();
                    undoManager.addEdit(compoundEdit);
                    undoManager.undo();
                       }
        catch (CannotUndoException cre) { cre.printStackTrace(); }
        updateButtons("4");
      }
    });
 
    m_redoButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
                undoManager.redo();
        }
        catch (CannotRedoException cre) { cre.printStackTrace(); }
        updateButtons("5");
      }
    });
 
    setSize(400,300);
    setVisible(true);
  }
 
  public void updateButtons(String str) {
    System.out.println("updatebutton" + str);
    m_undoButton.setText(compoundEdit.getUndoPresentationName());
    m_redoButton.setText(compoundEdit.getRedoPresentationName());
  }
 
  public static void main(String argv[]) {
    new UndoRedoTextApp();
  }
  
}
 

Thanks in advance

Reply via email to