Get or set the selection state of JCheckBox
import java.awt.FlowLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
public class CheckBoxState extends JFrame {
public CheckBoxState() {
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.LEFT));
JCheckBox checkBox = new JCheckBox("Check me!");
checkBox.setSelected(true);
boolean selected = checkBox.isSelected();
if (selected) {
System.out.println("Check box state is selected.");
} else {
System.out.println("Check box state is not selected.");
}
getContentPane().add(checkBox);
}
public static void main(String[] args) {
new CheckBoxState().setVisible(true);
}
}
Related examples in the same category