1

I would like to know if there is a way to detect if changes in the selection of a item in a swing JCombobox is done by a user (actively) or is causes by repopulating the Jcombobox.

I have to dynamically repopulate the items of the combobox based on other selection, this also invokes the actionPerformed event

so actionPerformed is invoked by:

  • selection changed by user
  • repopulating the jcombobox items.

how to tell the difference?

Thanks of helping !

1 Answer 1

2

No, not really.

A possible solution is to disable event notification while the combo box is updated. This can be done in (at least) one of two ways...

Firstly, you could physically remove the listener from the combo box, if you have a reference to it.

Secondly, you set a boolean flag, which when true, the listener would ignore the event.

For example...

4
  • yes it is, a) wrong described question, e.g. Robs ComboBoxTwo, b) or MutableComboBoxModel (incl. to override selection if required)
    – mKorbel
    Commented Jun 5, 2014 at 10:19
  • @mKorbel But you still don't know the source of the event. And instead of using a flag in the listeners you change contract of the model half a dozen one , half a dozen of the other Commented Jun 5, 2014 at 10:22
  • Thanks i solved the problem by remove the actionListener on the repopulating process, but i will look into suggestions that mKorbel suggested. Commented Jun 5, 2014 at 10:23
  • @JelteVerbree Makes for a third options ;) Commented Jun 5, 2014 at 10:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.