In this example we are going to see how to use Swing JCheckBox Events With ActionListener.
Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.AbstractButton; import javax.swing.JCheckBox; import javax.swing.JFrame; public class Main { public static void main(String args[]) { JFrame frame = new JFrame("CheckBox Action Listner "); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JCheckBox aCheckBox4 = new JCheckBox("Click Me"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { AbstractButton abstractButton = (AbstractButton) actionEvent.getSource(); boolean selected = abstractButton.getModel().isSelected(); System.out.println(selected); } }; aCheckBox4.addActionListener(actionListener); frame.add(aCheckBox4); frame.setSize(300, 200); frame.setVisible(true); } } |
Output:
1 2 3 4 | true false |