KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > JCheckBoxMenuItem


1 /*
2  * @(#)JCheckBoxMenuItem.java 1.55 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing;
8
9 import java.util.EventListener JavaDoc;
10
11 import java.awt.*;
12 import java.awt.event.*;
13 import java.awt.image.*;
14
15 import java.io.ObjectOutputStream JavaDoc;
16 import java.io.ObjectInputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18
19 import javax.swing.plaf.*;
20 import javax.accessibility.*;
21
22
23 /**
24  * A menu item that can be selected or deselected. If selected, the menu
25  * item typically appears with a checkmark next to it. If unselected or
26  * deselected, the menu item appears without a checkmark. Like a regular
27  * menu item, a check box menu item can have either text or a graphic
28  * icon associated with it, or both.
29  * <p>
30  * Either <code>isSelected</code>/<code>setSelected</code> or
31  * <code>getState</code>/<code>setState</code> can be used
32  * to determine/specify the menu item's selection state. The
33  * preferred methods are <code>isSelected</code> and
34  * <code>setSelected</code>, which work for all menus and buttons.
35  * The <code>getState</code> and <code>setState</code> methods exist for
36  * compatibility with other component sets.
37  * <p>
38  * For further information and examples of using check box menu items,
39  * see <a
40  href="http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html">How to Use Menus</a>,
41  * a section in <em>The Java Tutorial.</em>
42  * <p>
43  * <strong>Warning:</strong>
44  * Serialized objects of this class will not be compatible with
45  * future Swing releases. The current serialization support is
46  * appropriate for short term storage or RMI between applications running
47  * the same version of Swing. As of 1.4, support for long term storage
48  * of all JavaBeans<sup><font size="-2">TM</font></sup>
49  * has been added to the <code>java.beans</code> package.
50  * Please see {@link java.beans.XMLEncoder}.
51  *
52  * @beaninfo
53  * attribute: isContainer false
54  * description: A menu item which can be selected or deselected.
55  *
56  * @version 1.55 12/19/03
57  * @author Georges Saab
58  * @author David Karlton
59  */

60 public class JCheckBoxMenuItem extends JMenuItem JavaDoc implements SwingConstants JavaDoc,
61         Accessible
62 {
63     /**
64      * @see #getUIClassID
65      * @see #readObject
66      */

67     private static final String JavaDoc uiClassID = "CheckBoxMenuItemUI";
68
69     /**
70      * Creates an initially unselected check box menu item with no set text or icon.
71      */

72     public JCheckBoxMenuItem() {
73         this(null, null, false);
74     }
75
76     /**
77      * Creates an initially unselected check box menu item with an icon.
78      *
79      * @param icon the icon of the CheckBoxMenuItem.
80      */

81     public JCheckBoxMenuItem(Icon JavaDoc icon) {
82         this(null, icon, false);
83     }
84
85     /**
86      * Creates an initially unselected check box menu item with text.
87      *
88      * @param text the text of the CheckBoxMenuItem
89      */

90     public JCheckBoxMenuItem(String JavaDoc text) {
91         this(text, null, false);
92     }
93     
94     /**
95      * Creates a menu item whose properties are taken from the
96      * Action supplied.
97      *
98      * @since 1.3
99      */

100     public JCheckBoxMenuItem(Action JavaDoc a) {
101         this();
102     setAction(a);
103     }
104
105     /**
106      * Creates an initially unselected check box menu item with the specified text and icon.
107      *
108      * @param text the text of the CheckBoxMenuItem
109      * @param icon the icon of the CheckBoxMenuItem
110      */

111     public JCheckBoxMenuItem(String JavaDoc text, Icon JavaDoc icon) {
112     this(text, icon, false);
113     }
114
115     /**
116      * Creates a check box menu item with the specified text and selection state.
117      *
118      * @param text the text of the check box menu item.
119      * @param b the selected state of the check box menu item
120      */

121     public JCheckBoxMenuItem(String JavaDoc text, boolean b) {
122         this(text, null, b);
123     }
124
125     /**
126      * Creates a check box menu item with the specified text, icon, and selection state.
127      *
128      * @param text the text of the check box menu item
129      * @param icon the icon of the check box menu item
130      * @param b the selected state of the check box menu item
131      */

132     public JCheckBoxMenuItem(String JavaDoc text, Icon JavaDoc icon, boolean b) {
133     super(text, icon);
134         setModel(new JToggleButton.ToggleButtonModel JavaDoc());
135         setSelected(b);
136     setFocusable(false);
137     }
138
139     /**
140      * Returns the name of the L&F class
141      * that renders this component.
142      *
143      * @return "CheckBoxMenuItemUI"
144      * @see JComponent#getUIClassID
145      * @see UIDefaults#getUI
146      */

147     public String JavaDoc getUIClassID() {
148         return uiClassID;
149     }
150             
151      /**
152       * Returns the selected-state of the item. This method
153       * exists for AWT compatibility only. New code should
154       * use isSelected() instead.
155       *
156       * @return true if the item is selected
157       */

158     public boolean getState() {
159         return isSelected();
160     }
161             
162     /**
163      * Sets the selected-state of the item. This method
164      * exists for AWT compatibility only. New code should
165      * use setSelected() instead.
166      *
167      * @param b a boolean value indicating the item's
168      * selected-state, where true=selected
169      * @beaninfo
170      * description: The selection state of the check box menu item
171      * hidden: true
172      */

173     public synchronized void setState(boolean b) {
174         setSelected(b);
175     }
176             
177             
178     /**
179      * Returns an array (length 1) containing the check box menu item
180      * label or null if the check box is not selected.
181      *
182      * @return an array containing one Object -- the text of the menu item
183      * -- if the item is selected; otherwise null
184      */

185     public Object JavaDoc[] getSelectedObjects() {
186         if (isSelected() == false)
187             return null;
188         Object JavaDoc[] selectedObjects = new Object JavaDoc[1];
189         selectedObjects[0] = getText();
190         return selectedObjects;
191     }
192
193     /**
194      * See readObject() and writeObject() in JComponent for more
195      * information about serialization in Swing.
196      */

197     private void writeObject(ObjectOutputStream JavaDoc s) throws IOException JavaDoc {
198         s.defaultWriteObject();
199         if (getUIClassID().equals(uiClassID)) {
200             byte count = JComponent.getWriteObjCounter(this);
201             JComponent.setWriteObjCounter(this, --count);
202             if (count == 0 && ui != null) {
203                 ui.installUI(this);
204             }
205         }
206     }
207
208
209     /**
210      * Returns a string representation of this JCheckBoxMenuItem. This method
211      * is intended to be used only for debugging purposes, and the
212      * content and format of the returned string may vary between
213      * implementations. The returned string may be empty but may not
214      * be <code>null</code>.
215      *
216      * @return a string representation of this JCheckBoxMenuItem.
217      */

218     protected String JavaDoc paramString() {
219     return super.paramString();
220     }
221
222 /////////////////
223
// Accessibility support
224
////////////////
225

226     /**
227      * Gets the AccessibleContext associated with this JCheckBoxMenuItem.
228      * For JCheckBoxMenuItems, the AccessibleContext takes the form of an
229      * AccessibleJCheckBoxMenuItem.
230      * A new AccessibleJCheckBoxMenuItem instance is created if necessary.
231      *
232      * @return an AccessibleJCheckBoxMenuItem that serves as the
233      * AccessibleContext of this AccessibleJCheckBoxMenuItem
234      */

235     public AccessibleContext getAccessibleContext() {
236         if (accessibleContext == null) {
237             accessibleContext = new AccessibleJCheckBoxMenuItem();
238         }
239         return accessibleContext;
240     }
241
242     /**
243      * This class implements accessibility support for the
244      * <code>JCheckBoxMenuItem</code> class. It provides an implementation
245      * of the Java Accessibility API appropriate to checkbox menu item
246      * user-interface elements.
247      * <p>
248      * <strong>Warning:</strong>
249      * Serialized objects of this class will not be compatible with
250      * future Swing releases. The current serialization support is
251      * appropriate for short term storage or RMI between applications running
252      * the same version of Swing. As of 1.4, support for long term storage
253      * of all JavaBeans<sup><font size="-2">TM</font></sup>
254      * has been added to the <code>java.beans</code> package.
255      * Please see {@link java.beans.XMLEncoder}.
256      */

257     protected class AccessibleJCheckBoxMenuItem extends AccessibleJMenuItem {
258         /**
259          * Get the role of this object.
260          *
261          * @return an instance of AccessibleRole describing the role of the
262          * object
263          */

264         public AccessibleRole getAccessibleRole() {
265             return AccessibleRole.CHECK_BOX;
266         }
267     } // inner class AccessibleJCheckBoxMenuItem
268
}
269
Popular Tags