Swing/InheritsPopupMenu のバックアップソース(No.14)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/InheritsPopupMenu へ行く。
- 1 (2008-03-17 (月) 13:34:51)
- 2 (2008-03-28 (金) 20:17:58)
- 3 (2008-09-11 (木) 14:37:53)
- 4 (2008-11-07 (金) 19:52:17)
- 5 (2009-04-02 (木) 19:53:47)
- 6 (2010-11-15 (月) 15:40:30)
- 7 (2011-04-20 (水) 18:39:14)
- 8 (2013-01-24 (木) 00:49:30)
- 9 (2013-07-26 (金) 01:08:26)
- 10 (2014-12-26 (金) 15:51:10)
- 11 (2015-08-05 (水) 15:13:28)
- 12 (2017-04-08 (土) 18:28:08)
- 13 (2017-05-08 (月) 14:12:03)
- 14 (2018-04-27 (金) 21:57:55)
- 15 (2020-04-23 (木) 18:37:28)
- 16 (2021-10-26 (火) 23:58:30)
--- category: swing folder: InheritsPopupMenu title: JPopupMenuの取得を親に委譲 tags: [JPopupMenu, JScrollPane, JViewport, JTable, JTableHeader] author: aterai pubdate: 2008-03-17T13:34:51+09:00 description: 親コンポーネントに設定されているJPopupMenuを取得して、これを表示します。 image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTOe9ph-LI/AAAAAAAAAcc/iwxbgnjvxg8/s800/InheritsPopupMenu.png --- * 概要 [#summary] 親コンポーネントに設定されている`JPopupMenu`を取得して、これを表示します。 #download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTOe9ph-LI/AAAAAAAAAcc/iwxbgnjvxg8/s800/InheritsPopupMenu.png) * サンプルコード [#sourcecode] #code(link){{ JScrollPane scroll = new JScrollPane(table); scroll.setComponentPopupMenu(new TablePopupMenu()); //scroll.getViewport().setInheritsPopupMenu(true); // JDK 1.5 table.setInheritsPopupMenu(true); //table.getTableHeader().setInheritsPopupMenu(true); }} #code{{ private class TablePopupMenu extends JPopupMenu { private final Action deleteAction = new DeleteAction("delete", null); private final Action createAction = new CreateAction("add", null); public TablePopupMenu() { super(); add(createAction); addSeparator(); add(deleteAction); } @Override public void show(Component c, int x, int y) { deleteAction.setEnabled(table.getSelectedRowCount() > 0); super.show(c, x, y); } } }} * 解説 [#explanation] 上記のサンプルでは、`JScrollPane`に`setComponentPopupMenu(JPopupMenu)`メソッドで、ポップアップメニューを追加し、`JTable`の方は、`setInheritsPopupMenu(true)`とすることで、親の`JScrollPane`に設定したポップアップメニューを使用するようになっています。 `JDK 1.5`では、`JViewport`も`setInheritsPopupMenu(true)`とする必要がありましたが、`JDK 1.6`ではデフォルトが変更されているようです。 ---- - `JDK 1.6`では、`JTable`のヘッダも、`setInheritsPopupMenu(true)`で、`JScrollPane`からポップアップメニューを取得して表示可能 -- %%`JDK 1.6` + `WindowsLookAndFeel`で、`JTableHeader`上にポップアップメニューを表示すると、以下のようにうまく再描画できない場合がある%% 修正済み + ヘッダを右クリックしながら、右端にドラッグ、ポップアップ表示 + KBD{Esc}キーで、ポップアップ非表示 + ヘッダ上で右クリック、ポップアップ、KBD{Esc}キー // #ref(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTOhYdteZI/AAAAAAAAAcg/CzWZOSF9pVw/s800/InheritsPopupMenu1.png) * 参考リンク [#reference] - [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JComponent.html#setInheritsPopupMenu-boolean- JComponent#setInheritsPopupMenu(boolean) (Java Platform SE 8)] - [[JPopupMenuをコンポーネントに追加>Swing/ComponentPopupMenu]] * コメント [#comment] #comment #comment