1 /* Copyright (C) 2000 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
10 import java.awt.peer.CheckboxMenuItemPeer;
11 import java.awt.event.ItemListener;
12 import java.awt.event.ItemEvent;
14 /** This implements a menu item which keeps track of a boolean state.
15 * @author Tom Tromey <tromey@redhat.com>
16 * @date December 25, 2000
18 public class CheckboxMenuItem extends MenuItem implements ItemSelectable
20 /** Create a new CheckboxMenuItem.
21 * @param label The checkbox label. A null value is the same as "";
22 * null is the default.
23 * @param state The initial check state; defaults to false.
25 public CheckboxMenuItem ()
30 public CheckboxMenuItem (String label)
35 public CheckboxMenuItem (String label, boolean state)
41 /** Add a listener for item events.
42 * @param listener The listener to add.
44 public synchronized void addItemListener (ItemListener listener)
46 listeners = AWTEventMulticaster.add (listeners, listener);
49 /** This creates the component's peer. */
50 public void addNotify ()
54 // This choice of toolkit seems unsatisfying, but I'm not sure
56 peer = Toolkit.getDefaultToolkit ().createCheckboxMenuItem (this);
60 /** Returns this checkbox's label if this checkbox is selected. */
61 public Object[] getSelectedObjects ()
74 /** Returns the current state of this checkbox. */
75 public boolean getState ()
80 /** Generates a String representation of this Checkbox's state. */
81 public String paramString ()
83 return ("[" + getClass ().getName ()
84 + "state=" + state + ","
85 + "label=" + label + "]");
88 /** Process an event for this Checkbox.
89 * @param event The event the process.
91 protected void processEvent (AWTEvent event)
93 if (event instanceof ItemEvent)
94 processItemEvent ((ItemEvent) event);
96 super.processEvent (event);
99 /** Process an item event for this Checkbox.
100 * @param event The ItemEvent to process
102 protected void processItemEvent (ItemEvent event)
104 if (listeners != null)
105 listeners.itemStateChanged (event);
108 /** Remove an item listener.
109 * @param listener Item listener to remove.
111 public synchronized void removeItemListener (ItemListener listener)
113 listeners = AWTEventMulticaster.remove (listeners, listener);
116 /** Set the checkbox's state.
117 * @param state The new state.
119 public void setState (boolean state)
124 CheckboxMenuItemPeer cp = (CheckboxMenuItemPeer) peer;
132 ItemListener listeners;