Swing/DisableRightClick のバックアップソース(No.2)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/DisableRightClick へ行く。
- 1 (2009-06-29 (月) 10:14:32)
- 2 (2009-06-29 (月) 15:24:05)
- 3 (2009-07-10 (金) 14:40:52)
- 4 (2011-05-10 (火) 23:25:48)
- 5 (2011-05-20 (金) 15:51:37)
- 6 (2012-04-24 (火) 18:28:39)
- 7 (2012-04-24 (火) 19:28:45)
- 8 (2013-01-09 (水) 20:44:55)
- 9 (2013-05-03 (金) 23:49:12)
- 10 (2013-08-20 (火) 14:36:40)
- 11 (2014-11-01 (土) 00:46:09)
- 12 (2014-11-26 (水) 16:59:53)
- 13 (2014-11-30 (日) 00:51:50)
- 14 (2015-05-07 (木) 15:59:46)
- 15 (2017-03-09 (木) 16:37:10)
- 16 (2017-06-07 (水) 15:35:06)
- 17 (2017-11-02 (木) 15:34:40)
- 18 (2018-01-11 (木) 18:08:15)
- 19 (2018-02-24 (土) 19:51:30)
- 20 (2018-12-07 (金) 15:18:28)
- 21 (2019-05-22 (水) 19:35:38)
- 22 (2020-03-16 (月) 00:39:33)
- 23 (2021-06-13 (日) 01:25:41)
- 24 (2022-08-20 (土) 22:15:25)
TITLE:JComboBoxのドロップダウンリストで右クリックを無効化 #navi(../) RIGHT:Posted by [[terai]] at 2009-06-29 *JComboBoxのドロップダウンリストで右クリックを無効化 [#u3d32f35] JComboBoxのドロップダウンリスト(ポップアップメニュー)で、マウスの右クリックを無効にします。 -&jnlp; -&jar; -&zip; #screenshot **サンプルコード [#re92d6d4] #code{{ class BasicComboPopup2 extends BasicComboPopup { private Handler2 handler2; @Override public void uninstallingUI() { super.uninstallingUI(); handler2 = null; } public BasicComboPopup2(JComboBox combo) { super(combo); } protected MouseListener createListMouseListener() { if(handler2==null) handler2 = new Handler2(); return handler2; } private class Handler2 implements MouseListener{ public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) { if(e.getSource() == list) { if(list.getModel().getSize() > 0) { // <ins> if(!SwingUtilities.isLeftMouseButton(e) || !comboBox.isEnabled()) return; // </ins> // JList mouse listener if(comboBox.getSelectedIndex() == list.getSelectedIndex()) { comboBox.getEditor().setItem(list.getSelectedValue()); } comboBox.setSelectedIndex(list.getSelectedIndex()); } comboBox.setPopupVisible(false); // workaround for cancelling an edited item (bug 4530953) if(comboBox.isEditable() && comboBox.getEditor() != null) { comboBox.configureEditor(comboBox.getEditor(), comboBox.getSelectedItem()); } } } } } }} **解説 [#y202ff9d] 上記のサンプルでは、ComboBoxUI#createPopup()をオーバーライドして、ドロップダウンリストに設定するMouseListenerを入れ替えたBasicComboPopupを追加しています。 #code{{ combo02.setUI(new BasicComboBoxUI() { protected ComboPopup createPopup() { return new BasicComboPopup2( comboBox ); } }); }} 元のMouseListenerは、JComboBox全体のHandlerになっていますが、必要なのはドロップダウンリスト関係のみなので、e.getSource() == listな部分だけ元のHandlerからコピーし、この中でif(!SwingUtilities.isLeftMouseButton(e) || !comboBox.isEnabled()) return;と右クリックを無視しています。 //**参考リンク **コメント [#f7806cb9] #comment