Swing/ComponentPopupMenu のバックアップの現在との差分(No.3)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/ComponentPopupMenu へ行く。
- 1 (2008-03-10 (月) 01:39:57)
- 2 (2008-03-17 (月) 02:22:47)
- 3 (2008-03-21 (金) 19:04:29)
- 4 (2008-04-05 (土) 20:59:02)
- 5 (2008-04-10 (木) 18:58:52)
- 6 (2008-04-12 (土) 01:42:29)
- 7 (2011-05-04 (水) 19:00:01)
- 8 (2012-02-19 (日) 04:10:20)
- 9 (2013-01-24 (木) 01:09:16)
- 10 (2013-10-12 (土) 20:17:04)
- 11 (2014-11-01 (土) 00:46:09)
- 12 (2014-12-03 (水) 01:08:23)
- 13 (2015-04-14 (火) 17:14:54)
- 14 (2017-03-07 (火) 14:36:30)
- 15 (2017-11-02 (木) 15:34:40)
- 16 (2018-01-09 (火) 17:27:46)
- 17 (2018-09-20 (木) 21:36:53)
- 18 (2020-09-23 (水) 00:47:22)
- 19 (2022-05-24 (火) 10:29:56)
- 20 (2022-08-20 (土) 22:15:25)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JPopupMenuをコンポーネントに追加 #navi(../) *JPopupMenuをコンポーネントに追加 [#nc8dcfd3] >編集者:[[Terai Atsuhiro>terai]]~ 作成日:2008-03-10~ 更新日:&lastmod; --- category: swing folder: ComponentPopupMenu title: JPopupMenuをコンポーネントに追加 tags: [JPopupMenu, JTextComponent, PopupMenuListener, DefaultEditorKit] author: aterai pubdate: 2008-03-10T01:39:57+09:00 description: JComponentに右クリックなどでポップアップするJPopupMenuを追加します。 image: https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTKBw4_YtI/AAAAAAAAAVU/J_aFRLSj-VU/s800/ComponentPopupMenu.png --- * 概要 [#summary] `JComponent`に右クリックなどでポップアップする`JPopupMenu`を追加します。 #contents #download(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTKBw4_YtI/AAAAAAAAAVU/J_aFRLSj-VU/s800/ComponentPopupMenu.png) **概要 [#d2623802] JPopupMenuをコンポーネントに追加します。 * サンプルコード [#sourcecode] #code(link){{ JTextArea textArea = new JTextArea("ComponentPopupMenu Test"); textArea.setComponentPopupMenu(new TextComponentPopupMenu()); #screenshot **サンプルコード [#s7d9a0a6] #code{{ JTextArea textArea = new JTextArea("ComponentPopupMenu Test"); textArea.setComponentPopupMenu(new TextComponentPopupMenu(textArea)); }} #code{{ class TextComponentPopupMenu extends JPopupMenu { private final Action cutAction = new DefaultEditorKit.CutAction(); private final Action copyAction = new DefaultEditorKit.CopyAction(); private final Action pasteAction = new DefaultEditorKit.PasteAction(); private final Action deleteAction; private final Action selectAllAction; public TextComponentPopupMenu(final JTextComponent textArea) { public TextComponentPopupMenu() { super(); add(cutAction); add(copyAction); add(pasteAction); addSeparator(); add(deleteAction = new AbstractAction("delete") { public void actionPerformed(ActionEvent evt) { textArea.replaceSelection(null); @Override public void actionPerformed(ActionEvent evt) { ((JTextComponent) getInvoker()).replaceSelection(null); } }); addSeparator(); add(selectAllAction = new AbstractAction("select all") { public void actionPerformed(ActionEvent evt) { textArea.selectAll(); @Override public void actionPerformed(ActionEvent evt) { ((JTextComponent) getInvoker()).selectAll(); } }); // ActionMap am = textArea.getActionMap(); // add(cutAction = am.get("cut-to-clipboard")); // add(copyAction = am.get("copy-to-clipboard")); // add(am.get("paste-from-clipboard")); // addSeparator(); // add(deleteAction = am.get("delete-next")); // addSeparator(); // add(am.get("select-all")); } public void show(Component c, int x, int y) { JTextComponent textArea = (JTextComponent)c; boolean flg = textArea.getSelectedText()!=null; @Override public void show(Component c, int x, int y) { JTextComponent textArea = (JTextComponent) c; boolean flg = textArea.getSelectedText() != null; cutAction.setEnabled(flg); copyAction.setEnabled(flg); deleteAction.setEnabled(flg); super.show(c, x, y); } } }} -&jnlp; -&jar; -&zip; **解説 [#c9617b81] 上記のサンプルでは、JTextAreaにsetComponentPopupMenu(JPopupMenu)メソッドで、ポップアップメニューを追加しています。 * 解説 [#explanation] 上記のサンプルでは、`JTextArea`に`setComponentPopupMenu(JPopupMenu)`メソッドで、ポップアップメニューを追加しています。 JDK1.5でこのメソッドが追加されたため、各コンポーネントにマウスリスナーを設定して、e.isPopupTrigger()でポップアップを表示するクリックかを判断するといったコードを書く必要が無くなっています。 - `JDK 1.5`でこのメソッドが追加されたため、各コンポーネントにマウスリスナーを設定して`e.isPopupTrigger()`でポップアップを表示するクリックの判断が不要になった - ポップアップメニューを表示するときにコンポーネントの状態(例えば`JTable`や`JList`などでの行選択の有無や、テキストが選択されてるかとどうかなどの条件)でメニューが実行可か不可かを変更したい場合は、`JPopupMenu#show(Component, int, int)`メソッドをオーバーライドして使用可能 - このサンプルではテキストが選択されている場合だけカット、コピー、削除メニューが有効になるよう設定 ポップアップメニューを表示するときに、コンポーネントの状態(例えばJTableやJListなどでの行選択の有無や、テキストが選択されてるかとどうかなどの条件)で、 メニューが実行可か不可かを変更したい場合は、JPopupMenu#show(Component, int, int) メソッドをオーバーライドして使用します。 ---- - `JPopupMenu#show(Component, int, int)`のオーバーライドではなく、`PopupMenuListener`を使用する方法もある このサンプルでは、テキストが選択されている場合だけ、カット、コピー、削除メニューが有効になるよう設定しています。 #code{{ JPopupMenu popup = new JPopupMenu(); Action cutAction = new DefaultEditorKit.CutAction(); Action copyAction = new DefaultEditorKit.CopyAction(); Action pasteAction = new DefaultEditorKit.PasteAction(); Action deleteAction = new AbstractAction("delete") { @Override public void actionPerformed(ActionEvent e) { ((JTextComponent) getInvoker()).replaceSelection(null); } }; Action selectAllAction = new AbstractAction("select all") { @Override public void actionPerformed(ActionEvent e) { JPopupMenu p = (JPopupMenu) e.getSource(); ((JTextComponent) p.getInvoker()).selectAll(); } }; popup.add(cutAction); popup.add(copyAction); popup.add(pasteAction); popup.addSeparator(); popup.add(deleteAction); popup.addSeparator(); popup.add(selectAllAction); popup.addPopupMenuListener(new PopupMenuListener() { @Override public void popupMenuCanceled(PopupMenuEvent e) {} @Override public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {} @Override public void popupMenuWillBecomeVisible(PopupMenuEvent e) { JPopupMenu p = (JPopupMenu) e.getSource(); JTextComponent c = (JTextComponent) p.getInvoker(); boolean flg = c.getSelectedText() != null; cutAction.setEnabled(flg); copyAction.setEnabled(flg); deleteAction.setEnabled(flg); } }); textArea.setComponentPopupMenu(popup); }} **参考リンク [#rd20f140] -[[JPopupMenuの取得を親に委譲>Swing/InheritsPopupMenu]] - `PopupMenuEvent`からはマウスでクリックした位置を取得できない -- このため、`JTabbedPane`などでどのタブの上でポップアップが表示されるかなどを取得したい場合は`JPopupMenu#show(...)`をオーバーライドする必要がある -- [[JTabbedPaneでタブを追加削除>Swing/TabbedPane]] **コメント [#ob667bd4] * 参考リンク [#reference] - [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JComponent.html#setComponentPopupMenu-javax.swing.JPopupMenu- JComponent#setComponentPopupMenu(JPopupMenu) (Java Platform SE 8)] - [[JPopupMenuの取得を親に委譲>Swing/InheritsPopupMenu]] - [[JTabbedPaneでタブを追加削除>Swing/TabbedPane]] - [https://bugs.openjdk.org/browse/JDK-6675802 Bug ID: 6675802 Regression: heavyweight popups cause SecurityExceptions in applets] - [https://bugs.openjdk.org/browse/JDK-6299213 Bug ID: 6299213 The PopupMenu is not updated if the LAF is changed (incomplete fix of 4962731)] * コメント [#comment] #comment #comment