Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Java AWT Checkbox Explained With Example

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:

Next, we declare three Checkbox instances named cb1 , cb2 and cb3 . We also declare a Label control.

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.

Now our controls are ready. To display these controls in the Frame Window, we should add these controls to it using the add  method.

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:

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.

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:

5. Complete Source Code and Output

The complete code example is below:

Test Run and Output

AWT Checkbox Example
AWT Checkbox Example

When you run the example, resize the form to accommodate the Label when status is not completely visible.

Categories: AWT, Java

Tags: , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.