How to create a listener that fires when USER selects an item in JComboBox

I am looking for a listener that fires ONLY when the user, the one who uses the program, selects an item in JComboBox . I do not want to use an ActionListener or ItemListener , because they also fire when I select an item through a program. And I can not use MouseListener either because it only works when the JComboBox button is JComboBox , and not when an item is selected.

I was wondering what is the easiest way to do this? My decision is currently messy. When I modify the selected jcombobox element through code, I set the flag to true. And in my action listener, it is only executed if the flag is false.

+4
source share
1 answer

A) I would recommend that you temporarily remove the listener from a software choice.

B) If your software changes do not affect another GUI event, you can solve it as follows in an ugly / unreliable / error-prone / β€œcracked” way. Check EventQueue.isEventDispatchThread() to see if a click has been clicked on a GUI thread (user).

C) (I just re-read your question and saw that you already found the method described below. Basically, I would say that this (or the method described above) is your best alternative.)

Another option is to have a boolean flag called nonUserSelection that you set to true before you select the value programmatically, and reset is false. In the action listener you just add

 if (nonUserSelection) return; 
+1
source

All Articles