JCheckbox - ActionListener and ItemListener?

Are both ActionListener and ItemListener used to fire events using JCheckBox?

So, what is the difference between them, in which case one of them is preferable to the other?

+50
java swing itemlistener actionlistener jcheckbox
Mar 27 2018-12-12T00:
source share
5 answers

Both ItemListener as well as ActionListener , in the case of JCheckBox have the same behavior. However, the main difference ItemListener can be caused by calling setSelected(true) on the checkbox. Since coding practice does not register both ItemListener and ActionListener with JCheckBox to avoid inconsistency.

+45
Mar 27 2018-12-12T00:
source share

The difference is that the ActionEvent fired when the action is executed in the JCheckBox , that is, the state changes either by clicking on it with the mouse, or by using a space or a mnemonics. It does not really listen to changes in events selected or canceled by JCheckBox .

For example, if JCheckBox c1 (say) is added to ButtonGroup . Changing the state of another JCheckBoxes in a ButtonGroup will not trigger an ActionEvent on the other JCheckBox ; instead, an ItemEvent is ItemEvent .

Final words: A ItemEvent triggered even if the user deselects the checkbox by selecting another JCheckBox (when in the ButtonGroup ), however, the ActionEvent not generated, like instead of the ActionEvent only listens to whether the action is performed on the JCheckBox (to which only the ActionListener is registered) or not. He does not know about ButtonGroup and all other selection / ButtonGroup materials.

+23
Jul 10 '13 at 16:42
source share

For reference, here is sscce , which illustrates the difference. Console:

 SELECTED
 ACTION_PERFORMED
 DESELECTED
 ACTION_PERFORMED

the code:

 import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JPanel; /** @see http://stackoverflow.com/q/9882845/230513 */ public class Listeners { private void display() { JFrame f = new JFrame("Listeners"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JCheckBox b = new JCheckBox("JCheckBox"); b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println(e.getID() == ActionEvent.ACTION_PERFORMED ? "ACTION_PERFORMED" : e.getID()); } }); b.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { System.out.println(e.getStateChange() == ItemEvent.SELECTED ? "SELECTED" : "DESELECTED"); } }); JPanel p = new JPanel(); p.add(b); f.add(p); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new Listeners().display(); } }); } } 
+17
Mar 27 '12 at 4:52
source share

I use addActionListener for JButtons, while addItemListener more convenient for JToggleButton . Together with if(event.getStateChange()==ItemEvent.SELECTED) , in the latter case, I add events for whenever the JToggleButton is checked / not checked.

+3
Oct 22 '13 at
source share

I tested this myself and looked at all the answers to this post, and I don’t think they answered this question very well. I experimented myself to get a good answer (code below). You can trigger both events with both an ActionListener and an ItemListener in 100% of cases when the state changes either with a switch or checkbox, or with any other kind of Swing element that I accept, since it is an Object type. The only difference I can tell between these two listeners is the type of event object that returns with the listener. And you get the best event type with a checkbox using an ItemListener, not an ActionListener.

Inverse types ActionEvent and ItemEvent will have different methods that can be used when triggering an event type. In the code below, comments show the difference in .get methods for each type of return class.

The code below sets up a simple JPanel with JRadioButtons, JCheckBoxes and JLabel, which changes based on the button configurations. I installed all RadioButtons and CheckBoxes with both an event listener and an element listener. Then I wrote the Listener classes below using the ActionListener fully commented out because I tested it first in this experiment. You will notice that if you add this panel to the frame and display, all the radio buttons and flags will always work regardless of the Listener type, just comment out the methods in one and try the other and vice versa.

The type of return to implemented methods is the main difference between them. Both listeners fire events the same way. The explanation is slightly better in the comment above - the reason the flag should use the ItemListener over the ActionListener because of the return type of the event.

 package EventHandledClasses; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class RadioButtonsAndCheckBoxesTest extends JPanel{ JLabel display; String funny, serious, political; JCheckBox bold,italic; JRadioButton funnyQuote, seriousQuote, politicalQuote; ButtonGroup quotes; public RadioButtonsAndCheckBoxesTest(){ funny = "You are not ugly, you were just born... different"; serious = "Recommend powdered soap in prison!"; political = "Trump can eat a little Bernie, but will choke on his Birdie"; display = new JLabel(funny); Font defaultFont = new Font("Ariel",Font.PLAIN,20); display.setFont(defaultFont); bold = new JCheckBox("Bold",false); bold.setOpaque(false); italic = new JCheckBox("Italic",false); italic.setOpaque(false); //Color itemBackground = funnyQuote = new JRadioButton("Funny",true); funnyQuote.setOpaque(false); seriousQuote = new JRadioButton("Serious"); seriousQuote.setOpaque(false); politicalQuote = new JRadioButton("Political"); politicalQuote.setOpaque(false); quotes = new ButtonGroup(); quotes.add(funnyQuote); quotes.add(seriousQuote); quotes.add(politicalQuote); JPanel primary = new JPanel(); primary.setPreferredSize(new Dimension(550, 100)); Dimension standard = new Dimension(500, 30); JPanel radioButtonsPanel = new JPanel(); radioButtonsPanel.setPreferredSize(standard); radioButtonsPanel.setBackground(Color.green); radioButtonsPanel.add(funnyQuote); radioButtonsPanel.add(seriousQuote); radioButtonsPanel.add(politicalQuote); JPanel checkBoxPanel = new JPanel(); checkBoxPanel.setPreferredSize(standard); checkBoxPanel.setBackground(Color.green); checkBoxPanel.add(bold); checkBoxPanel.add(italic); primary.add(display); primary.add(radioButtonsPanel); primary.add(checkBoxPanel); //Add Action Listener To test Radio Buttons funnyQuote.addActionListener(new ActionListen()); seriousQuote.addActionListener(new ActionListen()); politicalQuote.addActionListener(new ActionListen()); //Add Item Listener to test Radio Buttons funnyQuote.addItemListener(new ItemListen()); seriousQuote.addItemListener(new ItemListen()); politicalQuote.addItemListener(new ItemListen()); //Add Action Listener to test Check Boxes bold.addActionListener(new ActionListen()); italic.addActionListener(new ActionListen()); //Add Item Listener to test Check Boxes bold.addItemListener(new ItemListen()); italic.addItemListener(new ItemListen()); //adds primary JPanel to this JPanel Object add(primary); } private class ActionListen implements ActionListener{ public void actionPerformed(ActionEvent e) { /* Different Get Methods from ItemEvent e.getWhen() e.getModifiers() e.getActionCommand()*/ /*int font=Font.PLAIN; if(bold.isSelected()){ font += Font.BOLD; } if(italic.isSelected()){ font += Font.ITALIC; } display.setFont(new Font("Ariel",font,20)); if(funnyQuote.isSelected()){ display.setText(funny); } if(seriousQuote.isSelected()){ display.setText(serious); } if(politicalQuote.isSelected()){ display.setText(political); }*/ } } private class ItemListen implements ItemListener { public void itemStateChanged(ItemEvent arg0) { /* Different Get Methods from ActionEvent arg0.getItemSelectable() arg0.getStateChange() arg0.getItem()*/ int font=Font.PLAIN; if(bold.isSelected()){ font += Font.BOLD; } if(italic.isSelected()){ font += Font.ITALIC; } display.setFont(new Font("Ariel",font,20)); if(funnyQuote.isSelected()){ display.setText(funny); } if(seriousQuote.isSelected()){ display.setText(serious); } if(politicalQuote.isSelected()){ display.setText(political); } } } } 
0
Mar 27 '16 at 19:26
source share



All Articles