Scenario
I have implemented logic, which is intended to update some internal variables, whenever users check certain JCheckBox UI items or select an option in a JComboBox.
My listeners look as follows:
ItemListener listener = event ->
{
if( event.getStateChange() == ItemEvent.ITEM_STATE_CHANGED )
{
// do some business logic
}
};
uiElement.addItemListener( listener );
I have observed that due to if( event.getStateChange() == ItemEvent.ITEM_STATE_CHANGED ) the business logic never gets triggered, even though item state is clearly changed.
My workaround for this is as follows:
JCheckBox: Removeif( event.getStateChange() == ItemEvent.ITEM_STATE_CHANGED )- it is sufficient not to do this check, as only one item event is firedJComboBox: Replaceif( event.getStateChange() == ItemEvent.ITEM_STATE_CHANGED )byif( event.getStateChange() == ItemEvent.SELECTED )- otherwise an additional event is fired and handled
Question
While this gets me where I need to go my question remains: What is ItemEvent.ITEM_STATE_CHANGED for? Why won't if( event.getStateChange() == ItemEvent.ITEM_STATE_CHANGED ) resolve to true, when I check a JCheckBox and select an item in my JComboBox?
The documentation reads:
This event id indicates that an item's state changed.
Isn't this true in this scenario?
I also checked other questions, such as this one - but they refer to the itemStateChanged event in general, not to the use of ItemEvent.ITEM_STATE_CHANGED.
Likely I misunderstood what ItemEvent.ITEM_STATE_CHANGED is for. What is its use?