Using ItemListener for CheckboxMenuItem

The following illustrates use of ItemListener with AWT CheckboxMenuItem.

import java.awt.*;
import java.awt.event.*;
class CheckboxMenuItemEvent extends Frame
CheckboxMenuItem c;
MenuBar mb;
Menu m;

    public CheckboxMenuItemEvent()
    private void createAndShowGUI()
        setTitle("CheckboxMenuItem with ItemListener demo");
        // Create menubar and menu
        mb=new MenuBar();
        m=new Menu("Menu");
        // Create simple CheckboxMenuItem
        c=new CheckboxMenuItem("Check me");
        // Add ItemListener
        c.addItemListener(new ItemListener(){
            public void itemStateChanged(ItemEvent ie)
                // Change frame title
                setTitle("You "+(c.getState()?"checked":"unchecked")+".");
        // Add checkboxmenuitem,menu and MenuBar
    public static void main(String args[])
        new CheckboxMenuItemEvent();

CheckboxMenuItemEvent(): Code illustrating use of ItemListener with AWT CheckboxMenuItem is written here.
A CheckboxMenuItem is a menu item that can be checked as you see in Notepad > Format > Wordwrap.

Using ItemListener for AWT CheckboxMenuItem

Next: Using ItemListener for AWT RadioButton
Previous: Using ItemListener for AWT Checkbox

Posted by at on

Tags: AWT Event Handling,