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:

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:
1 2 3 4 5 6 7 8 9 10 11 |
//Sample 06: Add Menu Items to Menu mnuDraw.add(siLine); mnuDraw.add(miLine); //Sample 10: Add Separator => mnuDraw.addSeparator(); //<= 10 mnuDraw.add(chkRect); mnuDraw.add(chkPentagon); mnuDraw.add(chkCircle); mnuHelp.add(help); mnuHelp.add(about); |
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:
1 2 3 4 |
//Sample 11a: CheckBox Menu Items CheckboxMenuItem chkRect; CheckboxMenuItem chkPentagon; CheckboxMenuItem chkCircle; |
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.
1 2 3 4 5 |
//Sample 11b: Modify MenuItem as CheckBoxMenu Item & Have //them as Class Member chkRect = new CheckboxMenuItem("Rectangle"); chkPentagon = new CheckboxMenuItem("Pentagon"); chkCircle = new CheckboxMenuItem("Circle"); |
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:
1 2 3 |
//Sample 13: Implement Item Listener public class FrameWin extends Frame implements WindowListener, ActionListener, ItemListener |
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.
1 2 3 4 5 |
//Sample 14:Change the Listener to Item Listener chkRect.addItemListener(this); chkPentagon.addItemListener(this); chkCircle.addItemListener(this); //<= 14 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@Override public void itemStateChanged(ItemEvent e) { //Sample 15: Report What item is Selected CheckboxMenuItem menuClicked = (CheckboxMenuItem) e.getItemSelectable(); String Sel = ""; if (menuClicked.getState() == true) Sel = "Selected"; else Sel = "UnSelected"; String str = menuClicked.getLabel() + " Item " + Sel; lblDisplay.setText(str); } |
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
Categories: AWT
Tags: addItemListener, addSeparator, CheckboxMenuItem, getItemSelectable, getState, itemStateChanged