Swing/GetAllPopupMenus のバックアップ差分(No.3)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/GetAllPopupMenus へ行く。
- 1 (2017-01-30 (月) 15:28:00)
- 2 (2017-01-30 (月) 18:24:06)
- 3 (2017-02-20 (月) 19:40:38)
- 4 (2017-02-28 (火) 17:51:53)
- 5 (2017-03-28 (火) 19:38:35)
- 6 (2017-04-07 (金) 13:51:51)
- 7 (2018-02-08 (木) 16:20:56)
- 8 (2018-02-15 (木) 14:23:42)
- 9 (2018-10-02 (火) 13:54:03)
- 10 (2020-10-02 (金) 10:17:49)
- 11 (2022-06-24 (金) 18:22:35)
- 12 (2024-02-02 (金) 12:25:39)
- 追加された行はこの色です。
- 削除された行はこの色です。
---
category: swing
folder: GetAllPopupMenus
title: MenuSelectionManagerですべてのJPopupMenuを取得する
tags: [JPopupMenu, Focus]
author: aterai
pubdate: 2017-01-30T15:17:50+09:00
description: MenuSelectionManagerですべてのJPopupMenuを取得し、任意のタイミングでそれらを非表示に切り替えます。
image: https://drive.google.com/uc?export=view&id=18mKH-3iW9D0-aw0doM7C-6-hFWoe-JMa7w
---
* 概要 [#summary]
`MenuSelectionManager`ですべての`JPopupMenu`を取得し、任意のタイミングでそれらを非表示に切り替えます。
#download(https://drive.google.com/uc?export=view&id=18mKH-3iW9D0-aw0doM7C-6-hFWoe-JMa7w)
* サンプルコード [#sourcecode]
#code(link){{
tabs.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_4, KeyEvent.CTRL_MASK), "next2");
tabs.getActionMap().put("next2", new AbstractAction() {
@Override public void actionPerformed(ActionEvent e) {
for (MenuElement m: MenuSelectionManager.defaultManager().getSelectedPath()) {
if (m instanceof JPopupMenu) {
((JPopupMenu) m).setVisible(false);
}
}
tabs.setSelectedIndex((tabs.getSelectedIndex() + 1) % tabs.getTabCount());
}
});
}}
* 解説 [#explanation]
- KBD{Ctrl+1}: `prev1`
-- `JTable`に設定した`JPopupMenu`を開いた状態で、`JTabbedPane`に設定したキー入力(`JComponent.WHEN_IN_FOCUSED_WINDOW`)によるタブ切替アクションを実行すると、`JPopupMenu`が開いたまま前のタブの選択が実行される
-- `requestFocusInWindow()`などでフォーカスを`JTable`以外に移動しても、`JPopupMenu`は閉じない
- KBD{Ctrl+2}: `next1`
-- `JTable`に設定した`JPopupMenu`を開いた状態(`JPopupMenu`にフォーカスがある)の場合、`JTabbedPane`に設定したキー入力(`JComponent.WHEN_FOCUSED`)によるタブ切替アクションは、実行することはできない
- KBD{Ctrl+3}: `prev2`
-- `JTabbedPane`に設定したキー入力(`JComponent.WHEN_IN_FOCUSED_WINDOW`)によるタブ切替アクションを実行する場合、直前にダミーのマウスプレスイベント発行してすべての`JPopupMenu`を閉じる
- KBD{Ctrl+4}: `next2`
-- `JTabbedPane`に設定したキー入力(`JComponent.WHEN_IN_FOCUSED_WINDOW`)によるタブ切替アクションを実行する場合、`MenuSelectionManager#getSelectedPath()`ですべての`JPopupMenu`を取得し、`JPopupMenu#setVisible(false)`で非表示にする
-- 参考: `javax/swing/plaf/basic/BasicPopupMenuUI.java`の`List<JPopupMenu> getPopups()`メソッド
----
- 以下のように、`LayeredPane#getComponentsInLayer(JLayeredPane.POPUP_LAYER)`で表示中の`JPopupMenu`を取得する方法もあるが、`JFrame`の外に表示される場合の`HeavyWeightWindow`を使用した`JPopupMenu`が取得できない
#code{{
JLayeredPane lp = ((JFrame) tabs.getTopLevelAncestor()).getLayeredPane();
for (Component c: lp.getComponentsInLayer(JLayeredPane.POPUP_LAYER)) {
for (Component p: ((Container) c).getComponents()) {
if (p instanceof JPopupMenu) {
p.setVisible(false);
}
}
}
}}
* 参考リンク [#reference]
- [http://stackoverflow.com/questions/41867173/jpopupmenu-not-closing-on-keyevent java - JPopupMenu not closing on keyevent - Stack Overflow]
* コメント [#comment]
#comment
#comment