/** * Add undo/redo support to a text component. Undo is bound to "ctrl-z", redo to "ctrl-y". * * @param textcomp The text component. */ public static void addUndoSupport(JTextComponent textcomp) { // Based on // http://www.exampledepot.com/egs/javax.swing.undo/UndoText.html final UndoManager undo = new UndoManager(); Document doc = textcomp.getDocument(); // Listen for undo and redo events doc.addUndoableEditListener( new UndoableEditListener() { public void undoableEditHappened(UndoableEditEvent evt) { undo.addEdit(evt.getEdit()); } }); // Create an undo action and add it to the text component textcomp .getActionMap() .put( "Undo", new AbstractAction("Undo") { private static final long serialVersionUID = -1616574389415095169L; public void actionPerformed(ActionEvent evt) { try { if (undo.canUndo()) { undo.undo(); } } catch (CannotUndoException e) { } } }); // Bind the undo action to ctl-Z textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo"); // Create a redo action and add it to the text component textcomp .getActionMap() .put( "Redo", new AbstractAction("Redo") { private static final long serialVersionUID = 58635276936990330L; public void actionPerformed(ActionEvent evt) { try { if (undo.canRedo()) { undo.redo(); } } catch (CannotRedoException e) { } } }); // Bind the redo action to ctl-Y textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo"); }
private void initUndoFeature() { Document doc = textcomp.getDocument(); // Listen for undo and redo events doc.addUndoableEditListener( new UndoableEditListener() { public void undoableEditHappened(UndoableEditEvent ev) { undoMenuItem.setEnabled(true); addEdit(ev.getEdit()); refreshEnabledStatus(); } }); // Create an undo action and add it to the text component textcomp .getActionMap() .put( "Undo", new AbstractAction("Undo") { // $NON-NLS-1$ //$NON-NLS-2$ public static final long serialVersionUID = 0L; public void actionPerformed(ActionEvent ev) { undo(); } }); // Bind the undo action to ctl-Z textcomp .getInputMap() .put(KeyStroke.getKeyStroke("control Z"), "Undo"); // $NON-NLS-1$ //$NON-NLS-2$ // Create a redo action and add it to the text component textcomp .getActionMap() .put( "Redo", new AbstractAction("Redo") { // $NON-NLS-1$ //$NON-NLS-2$ public static final long serialVersionUID = 0L; public void actionPerformed(ActionEvent ev) { redo(); } }); // Bind the redo action to ctl-Y textcomp .getInputMap() .put(KeyStroke.getKeyStroke("control Y"), "Redo"); // $NON-NLS-1$ //$NON-NLS-2$ }
/** add Undo and Redo bindings to a text component */ public static void addUndoFunctionality(JTextComponent text) { // Add an undo manager final UndoManager undo = new UndoManager(); Document doc = text.getDocument(); // Listen for undo and redo events doc.addUndoableEditListener( new UndoableEditListener() { public void undoableEditHappened(UndoableEditEvent evt) { undo.addEdit(evt.getEdit()); } }); // Create an undo action and add it to the text component text.getActionMap() .put( "Undo", new AbstractAction("Undo") { 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") { 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"); }
/** * Construct an undo listener with default key mappings. The default key mappings invoke the undo * and redo actions on <i>textArea</i> . * * <p>A typical usage pattern would be: <code> * JTextArea textArea = new JTextArea("testing"); * textArea.getDocument().addUndoableEditListener(new UndoListener(textArea)); * </code> * * @param textArea the text component that is being listened to and upon which undo/redo actions * will be performed */ public UndoListener(JTextComponent textArea) { // Set the mapping for shortcut keys; InputMap inputMap = textArea.getInputMap(); ActionMap actionMap = textArea.getActionMap(); // Ctrl-z or equivalent to undo. inputMap.put( KeyStroke.getKeyStroke(KeyEvent.VK_Z, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "undo"); actionMap.put("undo", _undoAction); // Ctrl-y or equivalent to redo inputMap.put( KeyStroke.getKeyStroke(KeyEvent.VK_Y, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "redo"); actionMap.put("redo", _redoAction); }