---
category: swing
folder: UndoManager
title: UndoManagerでJTextFieldのUndo、Redoを行う
tags: [JTextField, JTextComponent, UndoManager, ActionMap, Document]
author: aterai
pubdate: 2009-06-15T13:35:15+09:00
description: JTextFieldのドキュメントにUndoManagerを追加して、Undo、Redoを行います。
image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTWX1uwgqI/AAAAAAAAApI/zvwc9TUlj4E/s800/UndoManager.png
---
* Summary [#summary]
`JTextField`のドキュメントに`UndoManager`を追加して、`Undo`、`Redo`を行います。
#download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTWX1uwgqI/AAAAAAAAApI/zvwc9TUlj4E/s800/UndoManager.png)
* Source Code Examples [#sourcecode]
#code(link){{
private static void initUndoRedo(JTextComponent tc) {
UndoManager manager = new UndoManager();
tc.getDocument().addUndoableEditListener(manager);
tc.getActionMap().put("undo", new UndoAction(manager));
tc.getActionMap().put("redo", new RedoAction(manager));
InputMap imap = tc.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
imap.put(KeyStroke.getKeyStroke(
KeyEvent.VK_Z, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "undo");
imap.put(KeyStroke.getKeyStroke(
KeyEvent.VK_Y, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "redo");
}
private static class UndoAction extends AbstractAction {
private final UndoManager undoManager;
public UndoAction(UndoManager manager) {
super("undo");
this.undoManager = manager;
}
@Override public void actionPerformed(ActionEvent e) {
try {
undoManager.undo();
} catch (CannotUndoException ex) {
// ex.printStackTrace();
Toolkit.getDefaultToolkit().beep();
}
}
}
}}
* Description [#explanation]
* Description [#description]
- `Document#addUndoableEditListener(UndoManager)`メソッドを使用して`JTextField`に`UndoManager`を追加し、以下のキー入力で`Undo`、`Redo`が実行可能になるよう設定
-- `Undo`: KBD{Ctrl+Z}
-- `Redo`: KBD{Ctrl+Y}
* Reference [#reference]
- [https://docs.oracle.com/javase/tutorial/uiswing/components/generaltext.html#undo Implementing Undo and Redo (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)]
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/undo/UndoManager.html UndoManager (Java Platform SE 8)]
- [[UndoManagerを使用した文字列選択ペーストの動作を変更する>Swing/ReplaceUndoableEdit]]
* Comment [#comment]
#comment
#comment