TITLE:JComboBoxのドロップダウンリストで右クリックを無効化
Posted by at 2009-06-29

JComboBoxのドロップダウンリストで右クリックを無効化

JComboBoxのドロップダウンリスト(ポップアップメニュー)で、マウスの右クリックを無効にします。
  • category: swing folder: DisableRightClick title: JComboBoxのドロップダウンリストで右クリックを無効化 tags: [JComboBox, BasicComboPopup, MouseListener, JList] author: aterai pubdate: 2009-06-29T10:14:32+09:00 description: JComboBoxのドロップダウンリスト(ポップアップメニュー)で、マウスの右クリックを無効にします。 image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTLKTBLgQI/AAAAAAAAAXI/mV-Gw1hPSYU/s800/DisableRightClick.png hreflang:
       href: https://java-swing-tips.blogspot.com/2009/06/disable-right-click-in-jcombobox.html
       lang: en

概要

JComboBoxのドロップダウンリスト(ポップアップメニュー)で、マウスの右クリックを無効にします。
DisableRightClick.png

サンプルコード

#spanend
#spanadd
class BasicComboPopup2 extends BasicComboPopup {
#spanend
  private transient Handler2 handler2;

#spandel
**サンプルコード [#re92d6d4]
#spanend
#spandel
#code{{
#spanend
#spandel
class BasicComboPopup2 extends BasicComboPopup {
#spanend
  private Handler2 handler2;
  public BasicComboPopup2(JComboBox combo) {
    super(combo);
  }
#spanadd

#spanend
  @Override public void uninstallingUI() {
    super.uninstallingUI();
    handler2 = null;
  }
  public BasicComboPopup2(JComboBox combo) {
    super(combo);
  }
#spanadd

#spanend
  @Override protected MouseListener createListMouseListener() {
    if(handler2==null) handler2 = new Handler2();
    if (handler2 == null) {
      handler2 = new Handler2();
    }
    return handler2;
  }
  private class Handler2 implements MouseListener{
    @Override public void mouseEntered(MouseEvent e) {}
    @Override public void mouseExited(MouseEvent e)  {}
    @Override public void mouseClicked(MouseEvent e) {}
    @Override public void mousePressed(MouseEvent e) {}
#spanadd

#spanend
  private class Handler2 extends MouseAdapter {
    @Override public void mouseReleased(MouseEvent e) {
      if(e.getSource() == list) {
        if(list.getModel().getSize() > 0) {
      if (e.getSource().equals(list)) {
        if (list.getModel().getSize() > 0) {
          // <ins>
          if(!SwingUtilities.isLeftMouseButton(e) || !comboBox.isEnabled()) return;
          if (!SwingUtilities.isLeftMouseButton(e) || !comboBox.isEnabled()) {
            return;
          }
          // </ins>
          // JList mouse listener
          if(comboBox.getSelectedIndex() == list.getSelectedIndex()) {
          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) {
        if (comboBox.isEditable() && comboBox.getEditor() != null) {
          comboBox.configureEditor(comboBox.getEditor(), comboBox.getSelectedItem());
        }
      }
    }
  }
}
View in GitHub: Java, Kotlin

解説

上記のサンプルでは、ComboBoxUI#createPopup()をオーバーライドして、ドロップダウンリストに設定するMouseListenerを入れ替えたBasicComboPopupを追加しています。

解説

上記のサンプルでは、ComboBoxUI#createPopup()をオーバーライドして、ドロップダウンリストに設定するMouseListenerを入れ替えたBasicComboPopupを追加しています。
combo02.setUI(new BasicComboBoxUI() {
  @Override protected ComboPopup createPopup() {
    return new BasicComboPopup2( comboBox );
    return new BasicComboPopup2(comboBox);
  }
});
元のMouseListenerは、JComboBox全体のHandlerになっていますが、必要なのはドロップダウンリスト関係のみなので、e.getSource() == listな部分だけ元のHandlerからコピーし、この中でif(!SwingUtilities.isLeftMouseButton(e) || !comboBox.isEnabled()) return;と右クリックを無視しています。 元のMouseListenerは、JComboBox全体のHandlerになっていますが、必要なのはドロップダウンリスト関係のみなので、e.getSource() == listな部分だけ元のHandlerからコピーし、この中でif(!SwingUtilities.isLeftMouseButton(e) || !comboBox.isEnabled()) return;と右クリックを無視しています。

以下のような方法もあります。

class BasicComboPopup3 extends BasicComboPopup {
  public BasicComboPopup3(JComboBox combo) {
    super(combo);
  }
  @SuppressWarnings("unchecked")
  @Override protected JList createList() {
    return new JList(comboBox.getModel()) {
      @Override public void processMouseEvent(MouseEvent e) {
        if(SwingUtilities.isRightMouseButton(e)) return;
        if(e.isControlDown()) {
          // Fix for 4234053. Filter out the Control Key from the list. 
        if (SwingUtilities.isRightMouseButton(e)) {
          return;
        }
        MouseEvent ev = e;
        if (e.isControlDown()) {
          // Fix for 4234053. Filter out the Control Key from the list.
          // ie., don't allow CTRL key deselection.
          e = new MouseEvent((Component)e.getSource(), e.getID(), e.getWhen(), 
                     e.getModifiers() ^ InputEvent.CTRL_MASK,
                     e.getX(), e.getY(),
                     e.getXOnScreen(), e.getYOnScreen(),
                     e.getClickCount(),
                     e.isPopupTrigger(),
                     MouseEvent.NOBUTTON);
          Toolkit toolkit = Toolkit.getDefaultToolkit();
          ev = new MouseEvent(e.getComponent(), e.getID(), e.getWhen(),
                              //e.getModifiers() ^ InputEvent.CTRL_MASK,
                              e.getModifiers() ^ toolkit.getMenuShortcutKeyMask(),
                              e.getX(), e.getY(),
                              e.getXOnScreen(), e.getYOnScreen(),
                              e.getClickCount(),
                              e.isPopupTrigger(),
                              MouseEvent.NOBUTTON);
        }
        super.processMouseEvent(e);
        super.processMouseEvent(ev);
      }
    };
  }
}

コメント

参考リンク

コメント