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); } } } }
maximusg Mar 27 '16 at 19:26 2016-03-27 19:26
source share