For those who been using Swing to develop Desktop Application will undoubtedly find that it is one of the most flexible and API-rich toolkit available. However such power comes at a price of complexity due to many ways of accomplishing the same task. This post will talk about the common listeners that often confused both beginners and veteran alike.
Some components like JCheckBox and JComboBox have both ActionListener and ItemListener that fire event upon change in the selection state. Many thought that they are synonymous and interchangeable, and indeed the lack of understanding is often the source of confusion not only for those beginners in using Swing but also experienced Swing developer. The inappropiate use of ActionListener and ItemListener are often one of the common source of hard-to-find bugs.
Take for example of JCheckBox. In a way, both listeners for JCheckBox work similarly in that both will fire event upon change in state, whether by clicking or toggle by spacebar or programmatically through doClick() method (Similar to mouse click). One major difference though is that JCheckBox’s itemListener can be fired through setSelected(boolean) method which allows one to fire the event based on desired state whereas others will act only after the state is altered. So why is it important ? Consider when application startup, the GUI needed to configure for defined state, and using setSelected will trigger ItemListener. Note that setSelected is exclusive to ItemListener and has no effect on ActionListener. Do not register both ActionListener and ItemListener as both will be fired, landing the component in a random state (Note the listener event model does not guarantee the sequence of event trigger).
Put simply,
if setSelected(true) is called then JCheckBox’s state will be set to Selected, and then the ItemListener will be fired.
If doClick() is called then JCheckBox’s state will be toggled first (eg from => ), and the ActionListener will be fired.
For JCheckBox, read more at http://72.5.124.55/docs/books/tutorial/uiswing/components/button.html#checkbox
Similarly, JComboBox behaves differently for these two types of Listener. JComBoBox offers more options than JCheckBox. Read more at
http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html
Code <TestActionItemListener.java>
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class TestActionItemListener
{
public static void main(String[] _args)
{
JFrame _fra = new JFrame(“Testing”);
_fra.setSize(500, 500);
_fra.setLayout(new FlowLayout());
final JCheckBox _uiChkTestAction = new JCheckBox(“ActionListener”);
_uiChkTestAction.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent _ev)
{
System.out.println(“State by ActionListener: ” + _uiChkTestAction.isSelected());
}
});
final JCheckBox _uiChkTestItem = new JCheckBox(“ItemListener”);
_uiChkTestItem.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
System.out.println(“State by ItemListener: ” + _uiChkTestAction.isSelected());
}
});
_fra.add(_uiChkTestAction);
_fra.add(_uiChkTestItem);
_uiChkTestAction.setSelected(true);
_uiChkTestItem.setSelected(true);
_fra.setVisible(true);
}
}
i just don’t like ur program………..so next time i want to to see good………………
That was very nice. It’s exactly what was going wrong in my program. Thanks.
Gracias,
You did a great deal of help.
Keep it up
Thanks – the Javadoc doesn’t seem to explain this anywhere!
I believe you have an error in your program. In your _uiChkTestItem’s ItemListener, you print out the selected state of the _uiChkTestAction instead of the _uiChkTestItem’s state. Otherwise I found this example very helpful, thank you!