Java tutorial
//package com.java2s; import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.KeyStroke; import javax.swing.event.UndoableEditEvent; import javax.swing.event.UndoableEditListener; import javax.swing.text.JTextComponent; import javax.swing.undo.CannotRedoException; import javax.swing.undo.CannotUndoException; import javax.swing.undo.UndoManager; public class Main { public static void bindUndoManager(final JTextComponent text, final UndoManager undo) { text.getDocument().addUndoableEditListener(new UndoableEditListener() { @Override public void undoableEditHappened(UndoableEditEvent e) { undo.addEdit(e.getEdit()); } }); text.getActionMap().put("Undo", new AbstractAction("Undo") { private static final long serialVersionUID = 1L; public void actionPerformed(ActionEvent evt) { try { if (undo.canUndo()) { undo.undo(); } } catch (CannotUndoException e) { } } }); // Bind the undo action to ctl-Z text.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo"); // Create a redo action and add it to the text component text.getActionMap().put("Redo", new AbstractAction("Redo") { private static final long serialVersionUID = 1L; public void actionPerformed(ActionEvent evt) { try { if (undo.canRedo()) { undo.redo(); } } catch (CannotRedoException e) { } } }); // Bind the redo action to ctl-Y text.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo"); } }