1 /* 2 * Copyright (c) 1997, 2015, 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.beans.JavaBean; 28 import java.beans.BeanProperty; 29 import java.io.ObjectOutputStream; 30 import java.io.IOException; 31 32 import javax.accessibility.*; 33 34 /** 35 * An implementation of a radio button menu item. 36 * A <code>JRadioButtonMenuItem</code> is 37 * a menu item that is part of a group of menu items in which only one 38 * item in the group can be selected. The selected item displays its 39 * selected state. Selecting it causes any other selected item to 40 * switch to the unselected state. 41 * To control the selected state of a group of radio button menu items, 42 * use a <code>ButtonGroup</code> object. 43 * <p> 44 * Menu items can be configured, and to some degree controlled, by 45 * <code><a href="Action.html">Action</a></code>s. Using an 46 * <code>Action</code> with a menu item has many benefits beyond directly 47 * configuring a menu item. Refer to <a href="Action.html#buttonActions"> 48 * Swing Components Supporting <code>Action</code></a> for more 49 * details, and you can find more information in <a 50 * href="http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html">How 51 * to Use Actions</a>, a section in <em>The Java Tutorial</em>. 52 * <p> 53 * For further documentation and examples see 54 * <a 55 href="http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html">How to Use Menus</a>, 56 * a section in <em>The Java Tutorial.</em> 57 * <p> 58 * <strong>Warning:</strong> Swing is not thread safe. For more 59 * information see <a 60 * href="package-summary.html#threading">Swing's Threading 61 * Policy</a>. 62 * <p> 63 * <strong>Warning:</strong> 64 * Serialized objects of this class will not be compatible with 65 * future Swing releases. The current serialization support is 66 * appropriate for short term storage or RMI between applications running 67 * the same version of Swing. As of 1.4, support for long term storage 68 * of all JavaBeans™ 69 * has been added to the <code>java.beans</code> package. 70 * Please see {@link java.beans.XMLEncoder}. 71 * 72 * @author Georges Saab 73 * @author David Karlton 74 * @see ButtonGroup 75 * @since 1.2 76 */ 77 @JavaBean(description = "A component within a group of menu items which can be selected.") 78 @SwingContainer(false) 79 @SuppressWarnings("serial") // Same-version serialization only 80 public class JRadioButtonMenuItem extends JMenuItem implements Accessible { 81 /** 82 * @see #getUIClassID 83 * @see #readObject 84 */ 85 private static final String uiClassID = "RadioButtonMenuItemUI"; 86 87 /** 88 * Creates a <code>JRadioButtonMenuItem</code> with no set text or icon. 89 */ 90 public JRadioButtonMenuItem() { 91 this(null, null, false); 92 } 93 94 /** 95 * Creates a <code>JRadioButtonMenuItem</code> with an icon. 96 * 97 * @param icon the <code>Icon</code> to display on the 98 * <code>JRadioButtonMenuItem</code> 99 */ 100 public JRadioButtonMenuItem(Icon icon) { 101 this(null, icon, false); 102 } 103 104 /** 105 * Creates a <code>JRadioButtonMenuItem</code> with text. 106 * 107 * @param text the text of the <code>JRadioButtonMenuItem</code> 108 */ 109 public JRadioButtonMenuItem(String text) { 110 this(text, null, false); 111 } 112 113 /** 114 * Creates a radio button menu item whose properties are taken from the 115 * <code>Action</code> supplied. 116 * 117 * @param a the <code>Action</code> on which to base the radio 118 * button menu item 119 * 120 * @since 1.3 121 */ 122 public JRadioButtonMenuItem(Action a) { 123 this(); 124 setAction(a); 125 } 126 127 /** 128 * Creates a radio button menu item with the specified text 129 * and <code>Icon</code>. 130 * 131 * @param text the text of the <code>JRadioButtonMenuItem</code> 132 * @param icon the icon to display on the <code>JRadioButtonMenuItem</code> 133 */ 134 public JRadioButtonMenuItem(String text, Icon icon) { 135 this(text, icon, false); 136 } 137 138 /** 139 * Creates a radio button menu item with the specified text 140 * and selection state. 141 * 142 * @param text the text of the <code>CheckBoxMenuItem</code> 143 * @param selected the selected state of the <code>CheckBoxMenuItem</code> 144 */ 145 public JRadioButtonMenuItem(String text, boolean selected) { 146 this(text); 147 setSelected(selected); 148 } 149 150 /** 151 * Creates a radio button menu item with the specified image 152 * and selection state, but no text. 153 * 154 * @param icon the image that the button should display 155 * @param selected if true, the button is initially selected; 156 * otherwise, the button is initially unselected 157 */ 158 public JRadioButtonMenuItem(Icon icon, boolean selected) { 159 this(null, icon, selected); 160 } 161 162 /** 163 * Creates a radio button menu item that has the specified 164 * text, image, and selection state. All other constructors 165 * defer to this one. 166 * 167 * @param text the string displayed on the radio button 168 * @param icon the image that the button should display 169 * @param selected if {@code true}, the button is initially selected, 170 * otherwise, the button is initially unselected 171 */ 172 public JRadioButtonMenuItem(String text, Icon icon, boolean selected) { 173 super(text, icon); 174 setModel(new JToggleButton.ToggleButtonModel()); 175 setSelected(selected); 176 setFocusable(false); 177 } 178 179 /** 180 * Returns the name of the L&F class that renders this component. 181 * 182 * @return the string "RadioButtonMenuItemUI" 183 * @see JComponent#getUIClassID 184 * @see UIDefaults#getUI 185 */ 186 @BeanProperty(bound = false) 187 public String getUIClassID() { 188 return uiClassID; 189 } 190 191 /** 192 * See <code>readObject</code> and <code>writeObject</code> in 193 * <code>JComponent</code> for more 194 * information about serialization in Swing. 195 */ 196 private void writeObject(ObjectOutputStream s) throws IOException { 197 s.defaultWriteObject(); 198 if (getUIClassID().equals(uiClassID)) { 199 byte count = JComponent.getWriteObjCounter(this); 200 JComponent.setWriteObjCounter(this, --count); 201 if (count == 0 && ui != null) { 202 ui.installUI(this); 203 } 204 } 205 } 206 207 208 /** 209 * Returns a string representation of this 210 * <code>JRadioButtonMenuItem</code>. 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 217 * <code>JRadioButtonMenuItem</code> 218 */ 219 protected String paramString() { 220 return super.paramString(); 221 } 222 223 /** 224 * Overriden to return true, JRadioButtonMenuItem supports 225 * the selected state. 226 */ 227 boolean shouldUpdateSelectedStateFromAction() { 228 return true; 229 } 230 231 ///////////////// 232 // Accessibility support 233 //////////////// 234 235 /** 236 * Gets the AccessibleContext associated with this JRadioButtonMenuItem. 237 * For JRadioButtonMenuItems, the AccessibleContext takes the form of an 238 * AccessibleJRadioButtonMenuItem. 239 * A new AccessibleJRadioButtonMenuItem instance is created if necessary. 240 * 241 * @return an AccessibleJRadioButtonMenuItem that serves as the 242 * AccessibleContext of this JRadioButtonMenuItem 243 */ 244 @BeanProperty(bound = false) 245 public AccessibleContext getAccessibleContext() { 246 if (accessibleContext == null) { 247 accessibleContext = new AccessibleJRadioButtonMenuItem(); 248 } 249 return accessibleContext; 250 } 251 252 /** 253 * This class implements accessibility support for the 254 * <code>JRadioButtonMenuItem</code> class. It provides an 255 * implementation of the Java Accessibility API appropriate to 256 * <code>JRadioButtonMenuItem</code> user-interface elements. 257 * <p> 258 * <strong>Warning:</strong> 259 * Serialized objects of this class will not be compatible with 260 * future Swing releases. The current serialization support is 261 * appropriate for short term storage or RMI between applications running 262 * the same version of Swing. As of 1.4, support for long term storage 263 * of all JavaBeans™ 264 * has been added to the <code>java.beans</code> package. 265 * Please see {@link java.beans.XMLEncoder}. 266 */ 267 @SuppressWarnings("serial") // Same-version serialization only 268 protected class AccessibleJRadioButtonMenuItem extends AccessibleJMenuItem { 269 /** 270 * Get the role of this object. 271 * 272 * @return an instance of AccessibleRole describing the role of the 273 * object 274 */ 275 public AccessibleRole getAccessibleRole() { 276 return AccessibleRole.RADIO_BUTTON; 277 } 278 } // inner class AccessibleJRadioButtonMenuItem 279 }