Why is itemStateChanged on JComboBox is called twice when changed?

后端 未结 10 1869
忘掉有多难
忘掉有多难 2020-12-03 09:32

I\'m using a JComboBox with an ItemListener on it. When the value is changed, the itemStateChanged event is called twice. The first call, the ItemEvent is showing the origin

10条回答
  •  伪装坚强ぢ
    2020-12-03 10:11

    According to this thread,

    It gets tripped when you leave one result and then called again when set to another result

    Don't listen for itemStateChanged. Use an ActionListener instead, which is good for handling events of the combo.
    You need a ItemStateListener if you need to separately handle deselection / selection depending on the item involved.

    Changing the state of the item within itemStateChanged causes itemStateChanged to be fired... this called "reentrance".

提交回复
热议问题