Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make JComboBox selected item not changed when scrolling through its popuplist using keyboard

I have a JComboBox component in the panel and ItemListener attached to it. But it gets fired after every up/down keypress (when scrolling though opened popup list). I want to change the selected value after the user accepts selection by pressing for example Enter key.

This is not a case when using mouse. When I move mouse over the combobox's list the highlight follows mouse pointer, but selected item is not changed until I press the mouse button. I would like to have the same behavior for keyboard, i.e. moving highlight via up/down arrow does not change selected item, but pressing Enter does.

like image 674
toomyem Avatar asked Mar 04 '11 14:03

toomyem


2 Answers

I believe you should be able to do:

comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);

after you have created your comboBox instance to get this functionality

like image 59
tim_yates Avatar answered Sep 28 '22 04:09

tim_yates


In Java 8 they have fixed this behaviour, but only trigger if u set one UI property

UIManager.getLookAndFeelDefaults().put("ComboBox.noActionOnKeyNavigation", true);
like image 40
grigory mendel Avatar answered Sep 28 '22 04:09

grigory mendel