Swing/InheritsPopupMenu の変更点
- 追加された行はこの色です。
- 削除された行はこの色です。
- Swing/InheritsPopupMenu へ行く。
- Swing/InheritsPopupMenu の差分を削除
--- 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 [#summary] 親コンポーネントに設定されている`JPopupMenu`を取得して、これを表示します。 #download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTOe9ph-LI/AAAAAAAAAcc/iwxbgnjvxg8/s800/InheritsPopupMenu.png) * Source Code Examples [#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); // ... 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 [#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 [#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] #comment #comment