AWT CheckboxMenuItem Example

1. Introduction to AWT CheckboxMenuItem

We already know how to use the AWT Checkbox in a Frame Window. In the same way, we can use it as a menu item as well. Java AWT provides CheckboxMenuItem class, which serves the checkbox behaviour for a menu item. When the user clicks the menu item, it shows a tick mark when the containing menu is pulled down next time. This tick mark will alternate. Means, CheckboxMenuItem toggles between ON and OFF. Like, check box, CheckboxMenuItem raises ItemEvent and hence we can make use of the ItemListener to track the event.

2. About CheckboxMenuItem Example

In this example, we will learn how to use AWT CheckboxMenuItem and Menu Separator. The example we are going to create is shown below:

AWT CheckboxMenuItem Example
AWT CheckboxMenuItem Example

Here, we use our previous example on AWT Menu and change it to create CheckboxMenuItem. We create three checkbox menu items for Circle, Pentagon and Rectangle. In the above picture, we can also see a sunken line running between normal menu items and checked menu items. This line is called a Menu Separator. We create that here as well in this example.

3. Adding a Separator

We know CMenu exposes a method called add & using that we can add MenuItem to it. We did it in the past example while learning about AWT Menu. The same way we can add a separator. But the method name is addSeparator. This will add a line in the AWT Menu. Have a look at the code:

The code is from the past example on AWT Menu. Here, we added a separator at line 5. This will place a sunken line between the menu items line and rectangle.

4. Create AWT CheckboxMenuItem

We declare three CheckboxMenuItem references as class members. Later, we will add these members to the AWT Menu class. Below is the code:

Next, the constructor creates CheckboxMenuItem which we declared as a class member. Note, the constructor takes a string which shows up as a Label in the Menu when the menu items are displayed. Unlike the standard check box, Check box menu items just shows label only when they are in unchecked state. The containing menu shows a tick mark beside the label of the menu item, which will point out the user that the menu item is a CheckboxMenuItem, and it is currently in checked state.

5. Register with ItemListener

The ItemListener is for handling the Checkbox events. The same we can use for the CheckboxMenuItem as well. So, we will implement it for our AWT Frame Window class:

Next, we must register our three CheckboxMenuItem objects with the ItemListener. Since our FrameWin class will implement it, we pass ‘this’ reference to the addItemListener function. Now, when the user clicks any of these menu items, AWT will send the ItemEvent to the registered listener function.

6. Handle ItemEvent Of CheckboxMenuItem

Now, we will handle the ItemEvent by coding the contract function itemStateChanged of the ItemListener. Have a look at the code below:

In the above code, getItemSelectable method gives the CheckboxMenuItem reached out by the user. So, the user action can be either selecting the MenuItem and de-selecting it based on its current state. The getState method of the CheckboxMenuItem tells whether the menu item is in checked state or in unchecked state. Note, we also use the getLabel function on the AWT CheckboxMenuItem to get label caption and we use it to form a descriptive string. Finally, the message string is displayed on the Label control which tells what menu item the user clicked, and it is in checked state or not.

7. Watch AWT CheckboxMenuItem – Youtube Video

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.