-1

I have a JComboBox and it has an associated itemStageChanged method. The JComboxBox is updated in two ways:

  1. I call comboBox.setSelectedItem(...)
  2. The user selects an item in the comboBox via my GUI

I want only (2) to initiate an event. What method (e.g., actionPerformed? changeListener? itemListener? etc) should I use that will only catch (2) and not (1). Currently, itemStateChanged (even with an if statement to check if it is ItemEvent.SELECTED) is being called by (1) and (2).

class ItemChangeListener implements ItemListener{
    @Override
    public void itemStateChanged(ItemEvent event) {
       if (event.getStateChange() == ItemEvent.SELECTED) {
          //gets in here if user selects an item with their mouse 
          //or if setSelectedItem is called
       }
    }       
}
CodeGuy
  • 28,427
  • 76
  • 200
  • 317

1 Answers1

-1

Give this a try:

Boolean flag = false;

class ItemChangeListener implements ItemListener{
    @Override
    public void itemStateChanged(ItemEvent event) {
       // Condition for the desired event
       if (!flag && event.getStateChange() == ItemEvent.SELECTED) {
          Object item = event.getItem();
          // do something with object
       }
    }       
}
[...]
//Add the listener
myComboBox.addItemListener(new ItemChangeListener());
[...]
// Setting the selection
flag = true;
comboBox.setSelectedItem(...)
flag = false;

Taken from here.

Community
  • 1
  • 1
alex
  • 5,516
  • 2
  • 36
  • 60
  • Yes, this is what I currently have. This is called by (1) and (2). – CodeGuy Feb 04 '14 at 14:34
  • Ok, see my update. The point is, that the ActionListener doesn't see a difference beetwen the user and you setting a new selection, so you have to detect it on yourself. – alex Feb 04 '14 at 14:37
  • I don't think so, but maybe wait for some guys with more ui experience – alex Feb 04 '14 at 14:38
  • Your downvoting is not fair, not my code is hacky but your idea is hacky. Its absolutely right, that the ComboBox fires via the ActionListener, when an Item is selected by user or by your code, so you can only use a workaround – alex Feb 04 '14 at 14:41
  • I mean....You posted an answer which was incorrect, and then found a hack to cover your mistake. I apologize, but I downvoted this answer because it is simply not a correct answer. – CodeGuy Feb 04 '14 at 14:51
  • It works so its not wrong, your requirements need a hack because you already want to do a hack with abusing the itemlistener. Maybe you should think about the design of your UI then... And i corrected my answer, whats wrong with correcting mistakes? – alex Feb 04 '14 at 14:55
  • Do you really think this is the best way to do this? – CodeGuy Feb 04 '14 at 14:58
  • Do you really think, your idea is a good one? Your question shows a bad ui design to me, otherwise such an requirement wouldn't exist. If you want a different behaviour than the default you have to override the default behaviour or, to say it in your words, you have to hack it. If your requirement would be common, there would exist such a listener for your purpose in the standard implementation... – alex Feb 04 '14 at 15:03
  • I guess we will wait and see – CodeGuy Feb 04 '14 at 15:14