| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

JTextComponent 1

提供: MyMemoWiki
2020年2月15日 (土) 08:37時点におけるPiroto (トーク | 投稿記録)による版
ナビゲーションに移動 検索に移動

JTextComponent 1

Jython JTextComponent 1 | Jython Swing | Swing | Jython | Python |

内容

  • メニューとアクションの割付
  • キーボード押下との割付
  • Undo、Redoの実装
  • Documentの利用
  • Documentのイベント感知

ソースコード

実行例

0740 jtext.jpg

ソースコード
  1. import java.awt.BorderLayout;
  2. import java.awt.Event;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.KeyEvent;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7.  
  8. import javax.swing.AbstractAction;
  9. import javax.swing.Action;
  10. import javax.swing.InputMap;
  11. import javax.swing.JFrame;
  12. import javax.swing.JMenu;
  13. import javax.swing.JMenuBar;
  14. import javax.swing.JScrollPane;
  15. import javax.swing.JTextArea;
  16. import javax.swing.KeyStroke;
  17. import javax.swing.SwingUtilities;
  18. import javax.swing.event.UndoableEditEvent;
  19. import javax.swing.event.UndoableEditListener;
  20. import javax.swing.text.DefaultEditorKit;
  21. import javax.swing.text.Document;
  22. import javax.swing.text.JTextComponent;
  23. import javax.swing.undo.UndoManager;
  24.  
  25. /**
  26. * @see http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html#commands
  27. */
  28. public class JTextTest {
  29. private JFrame frame;
  30. private Map<Object, Action> actionMap = new HashMap<Object, Action>();
  31. private UndoManager undo = new UndoManager();
  32. private UndoAction undoAction;
  33. private RedoAction redoAction;
  34. private JMenu getMenu() {
  35. JMenu menu = new JMenu("Edit");
  36. menu.add(actionMap.get(DefaultEditorKit.cutAction));
  37. menu.add(actionMap.get(DefaultEditorKit.copyAction));
  38. menu.add(actionMap.get(DefaultEditorKit.pasteAction));
  39. menu.addSeparator();
  40. undoAction = new UndoAction();
  41. menu.add(undoAction);
  42. redoAction = new RedoAction();
  43. menu.add(redoAction);
  44. menu.addSeparator();
  45. menu.add(actionMap.get(DefaultEditorKit.selectAllAction));
  46. return menu;
  47. }
  48. private void createActionMap(JTextComponent txt) {
  49. Action[] actions = txt.getActions();
  50. for(Action action : actions) {
  51. this.actionMap.put(action.getValue(Action.NAME), action);
  52. }
  53. }
  54. private void createUI() {
  55. frame = new JFrame("TextTest");
  56. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  57.  
  58. JTextArea txtArea = new JTextArea(10, 30);
  59.  
  60. // Documentのイベント感知(Undo Redo 管理を行う)
  61. Document doc = txtArea.getDocument();
  62. doc.addUndoableEditListener(
  63. new UndoableEditListener() {
  64. public void undoableEditHappened(UndoableEditEvent e) {
  65. undo.addEdit(e.getEdit());
  66. undoAction.updateState();
  67. redoAction.updateState();
  68. }
  69. }
  70. );
  71. frame.getContentPane().add(new JScrollPane(txtArea), BorderLayout.CENTER);
  72. // TextComponentが持つActionをMapに格納
  73. createActionMap(txtArea);
  74. // メニューを作成
  75. JMenu menu = getMenu();
  76. JMenuBar mb = new JMenuBar();
  77. mb.add(menu);
  78. frame.setJMenuBar(mb);
  79. // テキストのUndo、Redoにキーを割り当てる (Ctrl+z,Ctrl+y)
  80. InputMap inputMap = txtArea.getInputMap();
  81. KeyStroke undoKey = KeyStroke.getKeyStroke(KeyEvent.VK_Z,Event.CTRL_MASK);
  82. KeyStroke redoKey = KeyStroke.getKeyStroke(KeyEvent.VK_Y,Event.CTRL_MASK);
  83. inputMap.put(undoKey, undoAction);
  84. inputMap.put(redoKey, redoAction);
  85. frame.pack();
  86. frame.setVisible(true);
  87. }
  88. public static void main(String[] args) {
  89. SwingUtilities.invokeLater(
  90. new Runnable(){
  91. public void run() {
  92. JTextTest jft = new JTextTest();
  93. jft.createUI();
  94. }
  95. }
  96. );
  97. }
  98. @SuppressWarnings("serial")
  99. class UndoAction extends AbstractAction {
  100. public UndoAction() {
  101. super("Undo");
  102. setEnabled(false);
  103. }
  104. public void actionPerformed(ActionEvent e) {
  105. try {
  106. undo.undo();
  107. } catch (Exception ex) {
  108. ex.printStackTrace();
  109. }
  110. updateState();
  111. // Redo を可能に
  112. redoAction.updateState();
  113. }
  114. public void updateState() {
  115. setEnabled(undo.canUndo());
  116. }
  117. }
  118. @SuppressWarnings("serial")
  119. class RedoAction extends AbstractAction {
  120. public RedoAction() {
  121. super("Redo");
  122. setEnabled(false);
  123. }
  124. public void actionPerformed(ActionEvent e) {
  125. try {
  126. undo.redo();
  127. } catch (Exception ex) {
  128. ex.printStackTrace();
  129. }
  130. updateState();
  131. }
  132. public void updateState() {
  133. setEnabled(undo.canRedo());
  134. }
  135. }
  136. }