Swing JCheckBox Events With ActionListener
Chapter:
Swing
Last Updated:
26-06-2016 15:36:25 UTC
Program:
/* ............... START ............... */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
public class JavaSwginJCheckBoxActionListner {
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);
}
}
/* ............... END ............... */
Output
true
false
Tags
Swing JCheckBox Events With ActionListener, Java, Swing