---
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を行います。
---
* 概要 [#o9568fb8]
`JTextField`のドキュメントに`UndoManager`を追加して、`Undo`、`Redo`を行います。

#download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTWX1uwgqI/AAAAAAAAApI/zvwc9TUlj4E/s800/UndoManager.png)

* サンプルコード [#n383e6ed]
#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, Event.CTRL_MASK), "undo");
  imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_Y, Event.CTRL_MASK), "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 cue) {
      //cue.printStackTrace();
      Toolkit.getDefaultToolkit().beep();
    }
  }
}
}}

* 解説 [#bc37965c]
`Document#addUndoableEditListener(UndoManager)`メソッドを使って、`JTextField`で`Undo`、`Redo`が以下のキー入力で実行できるように設定しています。

- `Undo` : KBD{Ctrl+Z}
- `Redo` : KBD{Ctrl+Y}

* 参考リンク [#k994a5a2]
- [http://docs.oracle.com/javase/tutorial/uiswing/components/generaltext.html#undo Implementing Undo and Redo (Text Component Features The Java™ Tutorialsg)]
- [[UndoManagerを使用した文字列選択ペーストの動作を変更する>Swing/ReplaceUndoableEdit]]

* コメント [#jdc94c6b]
#comment
#comment