Program to demonstrate working of CheckBox and adding ActionListener to it in Java's Swing Framework.
Output of the program :
package com.hubberspot.swing;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class CheckBoxDemo {
public static void main(String[] args) {
// 1. Create a simple frame by creating an object
// of JFrame.
JFrame frame = new JFrame();
// 2. Give the frame a title "CheckBox Demo" by calling
// setTitle method on frame object
frame.setTitle("CheckBox Demo");
// 3. Give frame a size in pixels as say 300,300
frame.setSize(300, 200);
// 4. set a operation when a user close the frame, here it
// closes the frame and exits the application
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 5. Create a checkbox by creating an Object of class
// JCheckBox.
final JCheckBox checkBox1 = new JCheckBox();
// 6. set the text of checkbox1 as "check box 1"
checkBox1.setText("check box 1");
// 7. Create another checkbox by creating an Object of class
// JCheckBox.
final JCheckBox checkBox2 = new JCheckBox();
// 8. set the text of checkBox2 as "check box 2"
checkBox2.setText("check box 2");
// 9. Make it selected that is (checked)
checkBox2.setSelected(true);
// 10. Create a label by creating an Object of class
// JLabel
final JLabel label = new JLabel();
// 11. set the text of label as "check box 2 selected"
// as initially we have made checkbox2 selected
label.setText("check box 2 selected");
// 12. We add an ActionListener to the checkbox1
// which will listen about the action user performs
// over the checkbox1. Here user only checks and unchecks
// it. As soon as user checks checkbox1 the text of the
// label changes from check box 2 selected to check box 1 selected
// and checkbox2 is unchecked
checkBox1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
JCheckBox checkBox = (JCheckBox) event.getSource();
if (checkBox.isSelected()) {
checkBox.setSelected(true);
checkBox2.setSelected(false);
label.setText("check box 1 selected");
}
}
});
// 13. We add an ActionListener to the checkbox2
// which will listen about the action user performs
// over the checkbox2. Here user only checks and unchecks
// it. As soon as user checks checkbox2 the text of the
// label changes from check box 1 selected to check box 2 selected
// and checkbox1 is unchecked
checkBox2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
JCheckBox checkBox = (JCheckBox) event.getSource();
if (checkBox.isSelected()) {
checkBox.setSelected(true);
checkBox1.setSelected(false);
label.setText("check box 2 selected");
}
}
});
// 14. for adding all these swing components to frame
// we first get the content pane which returns a
// Container
Container container = frame.getContentPane();
// 15. We create a Layout for Swing Components
// here we are using FlowLayout with value as "center'
// it will make the swing components float to center
FlowLayout layout = new FlowLayout(FlowLayout.CENTER);
// 16. We set the layout for container
container.setLayout(layout);
// 17. We add the checkboxes and label to it
container.add(checkBox1);
container.add(checkBox2);
container.add(label);
// 18. after adding checkboxes and label, we make it
// visible on the frame by calling the method as
// setVisible and passing value as true.
frame.setVisible(true);
}
}
Output of the program :

