Source Code: Download from Google Drive
1. About AWT Checkbox
We use the AWT Checkbox to get Yes or No kind of information from the User. For example, in a Frame Window they can respond to a specific field which need a choice of two, like American Citizen or Not. To go ahead with this example, first we need to create a Frame Window and you can refer the article here: Creating Frame Window – Hub Page Network Article.
2. Add Checkboxes to Frame Window
First, we import all the classes required for this example. Below is the code:
1 2 3 4 |
//Sample 07: Required Imports for this Example import java.awt.Checkbox; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; |
Next, we declare three Checkbox instances named cb1 , cb2 and cb3 . We also declare a Label control.
1 2 3 |
//Sample 08: Declare Checkboxes as members Checkbox cb1, cb2, cb3; Label label; |
After declaring the names, we create the objects using the new operator. In the below code, we create three instances of check boxes and one instance of a label control. Furthermore, we provide the display name for the control in the constructor. When Java render the control, it displays the string texts next to the check box control.
1 2 3 4 5 6 |
//Sample 09: Create Three CheckBoxes //and One Label cb1 = new Checkbox("Check Box 1"); cb2 = new Checkbox("Check Box 2"); cb3 = new Checkbox("Check Box 3"); label = new Label(); |
Now our controls are ready. To display these controls in the Frame Window, we should add these controls to it using the add method.
1 2 3 4 5 6 |
//Sample 10: Add Checkboxes and Label //to FrameWindow add(cb1); add(cb2); add(cb3); add(label); |
3. Checkbox ItemListener
The Frame Window has all the controls. Now, we need to add an event listener to the check box control. When the user clicks the check box button, it produces ItemEvent which we will explore later. Java AWT sends this event to the subscribed listener object. To make our Frame Window class an ItemListener , we need to implement the Item Listener interface. The below code shows this:
1 2 |
public class FrameWin extends Frame implements WindowListener, ItemListener |
Now the Frame Window can act as a Item Listener. We register this listener to each of our check box so that it can route the Item Event to our Frame Window. The addItemListener method of the check box registers the listener class.
1 2 3 4 |
//Sample 11: Add Listener for Checkbox cb1.addItemListener(this); cb2.addItemListener(this); cb3.addItemListener(this); |
4. Checkbox ItemEvent – Check if Checkbox is Checked
Every check box can have two states called checked and unchecked. In the label control, we will add a message describing which check box user clicked. We can also display the state of the check box as part of the message. Remember, our Frame windows class extends the ItemListener. This will force Frame Window to provide the implementation for public void itemStateChanged(ItemEvent e).
The getStateChange method of the ItemEvent can tell us whether a check box is in selected state or not. We can compare its return value with ItemEvent constants SELECTED or DESELECTED . In our example, we used the SELECTED constant. But how do we know the producer of the event as there are three check box and all registered to the same listener. The ItemEvent provides a method called getItemSelectable which returns the source of the event. Below is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//Sample 12: Implement Item Listener for //CheckBoxes public void itemStateChanged(ItemEvent e) { Checkbox selected = (Checkbox) e.getItemSelectable(); if (e.getStateChange() == ItemEvent.SELECTED) { label.setText(selected.getLabel() + " Checked"); } else { label.setText(selected.getLabel() + " UnChecked"); } } |
5. Complete Source Code and Output
The complete code example is below:
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Label; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; //Sample 07: Required Imports for this Example import java.awt.Checkbox; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; public class FrameWin extends Frame implements WindowListener, ItemListener { //Sample 08: Declare Checkboxes as members Checkbox cb1, cb2, cb3; Label label; //Sample 01: Constructor public FrameWin(String FrameTitle){ //Sample 02: Set Layout and Title super(FrameTitle); setLayout(new FlowLayout()); //Sample 09: Create Three CheckBoxes //and One Label cb1 = new Checkbox("Check Box 1"); cb2 = new Checkbox("Check Box 2"); cb3 = new Checkbox("Check Box 3"); label = new Label(); //Sample 10: Add Checkboxes and Label //to FrameWindow add(cb1); add(cb2); add(cb3); add(label); //Sample 11: Add Listener for Checkbox cb1.addItemListener(this); cb2.addItemListener(this); cb3.addItemListener(this); //Sample 03: Set Size of the Frame setSize(400, 300); setLocation(100,100); //Sample 04: Register with the Listener addWindowListener(this); } //Sample 05: Implement the Listeners public void windowOpened(WindowEvent e) {} public void windowClosing(WindowEvent e) { this.dispose(); } public void windowClosed(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} //Sample 12: Implement Item Listener for //CheckBoxes public void itemStateChanged(ItemEvent e) { Checkbox selected = (Checkbox) e.getItemSelectable(); if (e.getStateChange() == ItemEvent.SELECTED) { label.setText(selected.getLabel() + " Checked"); } else { label.setText(selected.getLabel() + " UnChecked"); } } } |
Test Run and Output

When you run the example, resize the form to accommodate the Label when status is not completely visible.
Tags: AWT.Checkbox, ItemEvent, ItemListener, itemStateChanges