Search code examples
javaswingpopupjcomboboxkeyevent

How to turn off auto selection in combobox while navigating in dropdown?


the Title states my problem almost completely.

I have some combo box classes which derive from JComboBox, additionally we use the PlasticUI from JGoodies. My Problem is that when I navigate through the available items in the drop down popup those items are automatically being selected. This only happens when I use the navigation keys, hovering with the mouse over the objects is fine. In my case this is pretty bad because it somehow provokes the lazy-loaded data in the object to be loaded and slow the combo box down immensely.

How can I turn this behavior off?

I tried debugging, but I cannot find a place to set a breakpoint properly, too much magic happening in the background :/

Plzz help :)


Solution

  • You can use the function ActionEvent.getModifiers() to check if the ItemChangeEvent got fired with the keyboard or the mouse.

    JCheckBox box = new JCheckBox();
    box.addActionListener(new ActionListener() {
    
        @Override
        public void actionPerformed(final ActionEvent e) {
            if (e.getModifiers() == 0) {
                System.out.println("keyboard");
            } else {
                System.out.println("mouse");
            }
        }
    });