1

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?

Legowaffles
  • 247
  • 2
  • 8
  • *"I would like to prevent an action event from being fired.."* Why? Or rather: Why not just ignore it in that case? Seems like you've put the wagon before the horse. – Andrew Thompson Jul 10 '13 at 11:51
  • 1
    It's easy, create one variable holding the selected value, after next selection, compare it with the selected value, it they are equals, *do nothing*, else, assign the selected value to it and *do your action*. – Azad Jul 10 '13 at 11:54
  • These JComboBox's are used to set values in other objects, which themselves will fire off additional events. The other problem is I was not aware this would happen until just recently, so my code doesn't test for this. Additionally, my model is such that it has "disabled" values. If I can find a way to do this, I can also prevent selection of these "disabled" values without that additional test everywhere. – Legowaffles Jul 10 '13 at 11:55
  • @Azad this still results in the event being fired, I would like to prevent it from even being fired. – Legowaffles Jul 10 '13 at 11:56
  • @Legowaffles: This is not possible, as swing components based on *object* and *event*, tell me, how do you know the selected value without using an event? You can't. Events creates for this reason. – Azad Jul 10 '13 at 12:00
  • @Andrew Thompson I'm disagree, thanks - hehehe Seems like you've put the wagon before the horse. :-), yes I love that ..... :-) – mKorbel Jul 10 '13 at 12:02
  • @Azad I fear I have not exactly been clear as to my intent. Hopefully the edits make it more clear. – Legowaffles Jul 10 '13 at 12:09
  • there are another sado_maso, to to add ListSelectionListener to derived JList from JComboBox, then is required to use Swing Action and to override isEnabled(), – mKorbel Jul 10 '13 at 12:12

1 Answers1

2

I would like to prevent an action event from being fired when a user selects a value already selected in a JComboBox.

  • use ItemListener,

  • wrap code into if (e.getStateChange() == ItemEvent.SELECTED) { as is shown, described in Oracle tutorial

  • for example

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • As per the original, question text, this is exactly the answer. Unfortunately, my intent was poorly communicated, so this does not exactly solve my problem. – Legowaffles Jul 10 '13 at 12:10
  • +1 for your answer that IMO *still* answers the actual need, if not the ..question. – Andrew Thompson Jul 10 '13 at 12:14
  • 1. see my comment to your question, 2. don't do that JComboBox is compound JComponent, fall apart out like beads, 3. re_read comment by @Andrew Thompson - Seems like you've put the wagon before the horse. 4. don't to simulate ItemListener – mKorbel Jul 10 '13 at 12:17
  • Actually, I just did some further reading. It looks like this will exactly solve my problem. I was under the impression that it would have a state of `ItemEvent.SELECTED` even if they reselected the current value. As such, marking this as the answer. Thank you. – Legowaffles Jul 10 '13 at 12:19