I would like to prevent an event from being fired when a user selects a value already selected in a JComboBox
.
For instance, assume I have a JComboBox
whose model has the following values:
- Cat
- Dog
- Fish
- Bird
- Snake
The currently selected value is "Cat". I would like to prevent an Listeners from being notified if the user selects "Cat" again, whilst "Cat" is already selected.
I have tried to implement this by adding a check in the setSelectedItem(Object)
in the model. This however did not work.
My next assumption is that if I want this functionality, I will need to subclass JComboBox
and override it's setSelectedItem(Object)
and contentsChanged(ListDataEvent)
functions.
Given the documentation for contentsChanged(ListDataEvent)
however, I am hesitant to override it. As such my question for all of you:
Is there a better way to get this desired functionality that doesn't require sub classing JComboBox
and overriding it's setSelectedItem(Object)
and contentsChanged(ListDataEvent)
functions?