2009/06/26

Disable Right Click In JComboBox-Dropdown-List

Code

class BasicComboPopup2 extends BasicComboPopup {
  private transient Handler2 handler2;

  public BasicComboPopup2(JComboBox combo) {
    super(combo);
  }

  @Override public void uninstallingUI() {
    super.uninstallingUI();
    handler2 = null;
  }

  @Override protected MouseListener createListMouseListener() {
    if (handler2 == null) {
      handler2 = new Handler2();
    }
    return handler2;
  }

  private class Handler2 extends MouseAdapter {
    @Override public void mouseReleased(MouseEvent e) {
      if (e.getSource().equals(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());
        }
      }
    }
  }
}

References

No comments:

Post a Comment