1 /* 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package javax.swing; 26 27 import java.util.EventListener; 28 29 import java.awt.*; 30 import java.awt.event.*; 31 import java.awt.image.*; 32 33 import java.io.ObjectOutputStream; 34 import java.io.ObjectInputStream; 35 import java.io.IOException; 36 37 import javax.swing.plaf.*; 38 import javax.accessibility.*; 39 40 /** 41 * An implementation of a radio button menu item. 42 * A <code>JRadioButtonMenuItem</code> is 43 * a menu item that is part of a group of menu items in which only one 44 * item in the group can be selected. The selected item displays its 45 * selected state. Selecting it causes any other selected item to 46 * switch to the unselected state. 47 * To control the selected state of a group of radio button menu items, 48 * use a <code>ButtonGroup</code> object. 49 * <p> 50 * Menu items can be configured, and to some degree controlled, by 51 * <code><a href="Action.html">Action</a></code>s. Using an 52 * <code>Action</code> with a menu item has many benefits beyond directly 53 * configuring a menu item. Refer to <a href="Action.html#buttonActions"> 54 * Swing Components Supporting <code>Action</code></a> for more 55 * details, and you can find more information in <a 56 * href="http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html">How 57 * to Use Actions</a>, a section in <em>The Java Tutorial</em>. 58 * <p> 59 * For further documentation and examples see 60 * <a 61 href="http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html">How to Use Menus</a>, 62 * a section in <em>The Java Tutorial.</em> 63 * <p> 64 * <strong>Warning:</strong> Swing is not thread safe. For more 65 * information see <a 66 * href="package-summary.html#threading">Swing's Threading 67 * Policy</a>. 68 * <p> 69 * <strong>Warning:</strong> 70 * Serialized objects of this class will not be compatible with 71 * future Swing releases. The current serialization support is 72 * appropriate for short term storage or RMI between applications running 73 * the same version of Swing. As of 1.4, support for long term storage 74 * of all JavaBeans™ 75 * has been added to the <code>java.beans</code> package. 76 * Please see {@link java.beans.XMLEncoder}. 77 * 78 * @beaninfo 79 * attribute: isContainer false 80 * description: A component within a group of menu items which can be selected. 81 * 82 * @author Georges Saab 83 * @author David Karlton 84 * @see ButtonGroup 85 */ 86 @SuppressWarnings("serial") // Same-version serialization only 87 public class JRadioButtonMenuItem extends JMenuItem implements Accessible { 88 /** 89 * @see #getUIClassID 90 * @see #readObject 91 */ 92 private static final String uiClassID = "RadioButtonMenuItemUI"; 93 94 /** 95 * Creates a <code>JRadioButtonMenuItem</code> with no set text or icon. 96 */ 97 public JRadioButtonMenuItem() { 98 this(null, null, false); 99 } 100 101 /** 102 * Creates a <code>JRadioButtonMenuItem</code> with an icon. 103 * 104 * @param icon the <code>Icon</code> to display on the 105 * <code>JRadioButtonMenuItem</code> 106 */ 107 public JRadioButtonMenuItem(Icon icon) { 108 this(null, icon, false); 109 } 110 111 /** 112 * Creates a <code>JRadioButtonMenuItem</code> with text. 113 * 114 * @param text the text of the <code>JRadioButtonMenuItem</code> 115 */ 116 public JRadioButtonMenuItem(String text) { 117 this(text, null, false); 118 } 119 120 /** 121 * Creates a radio button menu item whose properties are taken from the 122 * <code>Action</code> supplied. 123 * 124 * @param a the <code>Action</code> on which to base the radio 125 * button menu item 126 * 127 * @since 1.3 128 */ 129 public JRadioButtonMenuItem(Action a) { 130 this(); 131 setAction(a); 132 } 133 134 /** 135 * Creates a radio button menu item with the specified text 136 * and <code>Icon</code>. 137 * 138 * @param text the text of the <code>JRadioButtonMenuItem</code> 139 * @param icon the icon to display on the <code>JRadioButtonMenuItem</code> 140 */ 141 public JRadioButtonMenuItem(String text, Icon icon) { 142 this(text, icon, false); 143 } 144 145 /** 146 * Creates a radio button menu item with the specified text 147 * and selection state. 148 * 149 * @param text the text of the <code>CheckBoxMenuItem</code> 150 * @param selected the selected state of the <code>CheckBoxMenuItem</code> 151 */ 152 public JRadioButtonMenuItem(String text, boolean selected) { 153 this(text); 154 setSelected(selected); 155 } 156 157 /** 158 * Creates a radio button menu item with the specified image 159 * and selection state, but no text. 160 * 161 * @param icon the image that the button should display 162 * @param selected if true, the button is initially selected; 163 * otherwise, the button is initially unselected 164 */ 165 public JRadioButtonMenuItem(Icon icon, boolean selected) { 166 this(null, icon, selected); 167 } 168 169 /** 170 * Creates a radio button menu item that has the specified 171 * text, image, and selection state. All other constructors 172 * defer to this one. 173 * 174 * @param text the string displayed on the radio button 175 * @param icon the image that the button should display 176 */ 177 public JRadioButtonMenuItem(String text, Icon icon, boolean selected) { 178 super(text, icon); 179 setModel(new JToggleButton.ToggleButtonModel()); 180 setSelected(selected); 181 setFocusable(false); 182 } 183 184 /** 185 * Returns the name of the L&F class that renders this component. 186 * 187 * @return the string "RadioButtonMenuItemUI" 188 * @see JComponent#getUIClassID 189 * @see UIDefaults#getUI 190 */ 191 public String getUIClassID() { 192 return uiClassID; 193 } 194 195 /** 196 * See <code>readObject</code> and <code>writeObject</code> in 197 * <code>JComponent</code> for more 198 * information about serialization in Swing. 199 */ 200 private void writeObject(ObjectOutputStream s) throws IOException { 201 s.defaultWriteObject(); 202 if (getUIClassID().equals(uiClassID)) { 203 byte count = JComponent.getWriteObjCounter(this); 204 JComponent.setWriteObjCounter(this, --count); 205 if (count == 0 && ui != null) { 206 ui.installUI(this); 207 } 208 } 209 } 210 211 212 /** 213 * Returns a string representation of this 214 * <code>JRadioButtonMenuItem</code>. This method 215 * is intended to be used only for debugging purposes, and the 216 * content and format of the returned string may vary between 217 * implementations. The returned string may be empty but may not 218 * be <code>null</code>. 219 * 220 * @return a string representation of this 221 * <code>JRadioButtonMenuItem</code> 222 */ 223 protected String paramString() { 224 return super.paramString(); 225 } 226 227 /** 228 * Overriden to return true, JRadioButtonMenuItem supports 229 * the selected state. 230 */ 231 boolean shouldUpdateSelectedStateFromAction() { 232 return true; 233 } 234 235 ///////////////// 236 // Accessibility support 237 //////////////// 238 239 /** 240 * Gets the AccessibleContext associated with this JRadioButtonMenuItem. 241 * For JRadioButtonMenuItems, the AccessibleContext takes the form of an 242 * AccessibleJRadioButtonMenuItem. 243 * A new AccessibleJRadioButtonMenuItem instance is created if necessary. 244 * 245 * @return an AccessibleJRadioButtonMenuItem that serves as the 246 * AccessibleContext of this JRadioButtonMenuItem 247 */ 248 public AccessibleContext getAccessibleContext() { 249 if (accessibleContext == null) { 250 accessibleContext = new AccessibleJRadioButtonMenuItem(); 251 } 252 return accessibleContext; 253 } 254 255 /** 256 * This class implements accessibility support for the 257 * <code>JRadioButtonMenuItem</code> class. It provides an 258 * implementation of the Java Accessibility API appropriate to 259 * <code>JRadioButtonMenuItem</code> user-interface elements. 260 * <p> 261 * <strong>Warning:</strong> 262 * Serialized objects of this class will not be compatible with 263 * future Swing releases. The current serialization support is 264 * appropriate for short term storage or RMI between applications running 265 * the same version of Swing. As of 1.4, support for long term storage 266 * of all JavaBeans™ 267 * has been added to the <code>java.beans</code> package. 268 * Please see {@link java.beans.XMLEncoder}. 269 */ 270 @SuppressWarnings("serial") // Same-version serialization only 271 protected class AccessibleJRadioButtonMenuItem extends AccessibleJMenuItem { 272 /** 273 * Get the role of this object. 274 * 275 * @return an instance of AccessibleRole describing the role of the 276 * object 277 */ 278 public AccessibleRole getAccessibleRole() { 279 return AccessibleRole.RADIO_BUTTON; 280 } 281 } // inner class AccessibleJRadioButtonMenuItem 282 }