Reputation: 69
I'm doing practices of a Java book and I came up to an event handling code like below. My question is if itemStateChanged method called when an item is selected from a JComboBox then why we should ensure that an item is selected?
@Override
public void itemStateChanged(ItemEvent event)
{
if(event.getStateChange()
== ItemEvent.SELECTED)
label.setIcon(icons[
imagesJComboBox.getSelectedIndex()]);
}
Upvotes: 0
Views: 41
Reputation: 44456
It's a good practice to check if the method supposed to be called on a particular event really matches the correct event.
Since the method is public
, it can be used within any other situation or event. The call of the method itself doesn't secure the event maches ItemEvent.SELECTED
itself.
Upvotes: 1