JCheckbox - ActionListener and ItemListener?

TU_HEO DAKAI picture TU_HEO DAKAI · Mar 27, 2012 · Viewed 112.6k times · Source

Both ActionListener and ItemListener are used to fire an event with JCheckBox?

So, what's the difference between them and in which case one of them is preferred to the other?

Answer

NiranjanBhat picture NiranjanBhat · Mar 27, 2012

Both ItemListener as well as ActionListener, in case of JCheckBox have the same behaviour. However, major difference is ItemListener can be triggered by calling the setSelected(true) on the checkbox. As a coding practice do not register both ItemListener as well as ActionListener with the JCheckBox, in order to avoid inconsistency.