Swing/ComboBoxEnterSelectablePopup のバックアップ(No.4)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/ComboBoxEnterSelectablePopup へ行く。
- 1 (2018-02-15 (木) 14:23:42)
- 2 (2018-07-10 (火) 14:01:27)
- 3 (2020-07-07 (火) 14:19:44)
- 4 (2021-12-09 (木) 10:26:45)
- 5 (2022-05-29 (日) 01:18:15)
- 6 (2022-05-30 (月) 00:43:38)
- 7 (2023-02-07 (火) 12:15:47)
- 8 (2025-01-03 (金) 08:57:02)
- 9 (2025-01-03 (金) 09:01:23)
- 10 (2025-01-03 (金) 09:02:38)
- 11 (2025-01-03 (金) 09:03:21)
- 12 (2025-01-03 (金) 09:04:02)
- category: swing folder: ComboBoxEnterSelectablePopup title: JComboBoxのポップアップメニューでEnterキーが入力された場合のActionListenerの動作をテストする tags: [JComboBox, ActionListener, UIManager] author: aterai pubdate: 2017-07-10T15:41:36+09:00 description: 編集可能なJComboBoxのポップアップメニューでEnterキーが入力された場合のActionListenerの動作をテストします。 image: https://drive.google.com/uc?id=1IRqx7XfCe8R_uKdyEiatC5Ro-ucy_GPnZw
概要
編集可能なJComboBox
のポップアップメニューでEnterキーが入力された場合のActionListener
の動作をテストします。
Screenshot

Advertisement
サンプルコード
解説
ComboBox.isEnterSelectablePopup: false(default)
- ポップアップメニューが閉じている状態で、Enterキー入力やフォーカス移動で編集を終了するとアクションイベントが
2
回発生する- このアクションイベントで
JComboBox#getSelectedItem()
を実行するとエディタの値が取得される
- このアクションイベントで
- ポップアップメニューが開いている状態で、Enterキーを入力するとアクションイベントが
1
回発生する- 編集不可の
JComboBox
の場合と同じ動作 - このアクションイベントで
JComboBox#getSelectedItem()
を実行するとリストアイテムの値が取得される
- 編集不可の
- ポップアップメニューが閉じている状態で、Enterキー入力やフォーカス移動で編集を終了するとアクションイベントが
ComboBox.isEnterSelectablePopup: true
- ポップアップメニューが閉じている状態で、Enterキー入力やフォーカス移動で編集を終了するとアクションイベントが
2
回発生する- このアクションイベントで
JComboBox#getSelectedItem()
を実行するとエディタの値が取得される
- このアクションイベントで
- ポップアップメニューが開いている状態で、Enterキーを入力するとアクションイベントが
2
回発生する- このアクションイベントで
JComboBox#getSelectedItem()
を実行するとリストアイテムの値が取得される
- このアクションイベントで
- 詳細は
javax/swing/plaf/basic/BasicComboBoxUI.java
を参照// Forces the selection of the list item boolean isEnterSelectablePopup = UIManager.getBoolean("ComboBox.isEnterSelectablePopup"); if (!comboBox.isEditable() || isEnterSelectablePopup || ui.isTableCellEditor) { Object listItem = ui.popup.getList().getSelectedValue(); if (listItem != null) { // Use the selected value from popup // to set the selected item in combo box, // but ensure before that JComboBox.actionPerformed() // won't use editor's value to set the selected item comboBox.getEditor().setItem(listItem); comboBox.setSelectedItem(listItem); } } comboBox.setPopupVisible(false);
- ポップアップメニューが閉じている状態で、Enterキー入力やフォーカス移動で編集を終了するとアクションイベントが
- 注:
JComboBox#addItemListener(...)
で追加したItemListener
にはこの設定は影響しないUIManager.getBoolean("ComboBox.noActionOnKeyNavigation") == true
の場合、この設定は無視されComboBox.isEnterSelectablePopup: true
と同じ動作になる- ポップアップメニューが開いている状態で、Enterキーを入力するとアクションイベントが
2
回発生する
- ポップアップメニューが開いている状態で、Enterキーを入力するとアクションイベントが
ComboBox.isEnterSelectablePopup
の設定はEnterキーが入力されると毎回UIManager.getBoolean("ComboBox.isEnterSelectablePopup")
で取得されるので、切替はPopupMenuListener#popupMenuWillBecomeVisible(...)
メソッドをオーバーライドして実行