1 7 package javax.swing; 8 9 import java.awt.*; 10 import java.awt.event.*; 11 import java.beans.*; 12 13 import javax.swing.plaf.*; 14 import javax.accessibility.*; 15 16 import java.io.ObjectOutputStream ; 17 import java.io.ObjectInputStream ; 18 import java.io.IOException ; 19 20 21 58 public class JRadioButton extends JToggleButton implements Accessible { 59 60 64 private static final String uiClassID = "RadioButtonUI"; 65 66 67 71 public JRadioButton () { 72 this(null, null, false); 73 } 74 75 81 public JRadioButton(Icon icon) { 82 this(null, icon, false); 83 } 84 85 91 public JRadioButton(Action a) { 92 this(); 93 setAction(a); 94 } 95 96 104 public JRadioButton(Icon icon, boolean selected) { 105 this(null, icon, selected); 106 } 107 108 113 public JRadioButton (String text) { 114 this(text, null, false); 115 } 116 117 125 public JRadioButton (String text, boolean selected) { 126 this(text, null, selected); 127 } 128 129 136 public JRadioButton(String text, Icon icon) { 137 this(text, icon, false); 138 } 139 140 147 public JRadioButton (String text, Icon icon, boolean selected) { 148 super(text, icon, selected); 149 setBorderPainted(false); 150 setHorizontalAlignment(LEADING); 151 } 152 153 154 159 public void updateUI() { 160 setUI((ButtonUI)UIManager.getUI(this)); 161 } 162 163 164 175 public String getUIClassID() { 176 return uiClassID; 177 } 178 179 180 192 protected void configurePropertiesFromAction(Action a) { 193 String [] types = { Action.MNEMONIC_KEY, Action.NAME, 194 Action.SHORT_DESCRIPTION, 195 Action.ACTION_COMMAND_KEY, "enabled" }; 196 configurePropertiesFromAction(a, types); 197 } 198 199 216 protected PropertyChangeListener createActionPropertyChangeListener(Action a) { 217 return new AbstractActionPropertyChangeListener (this, a) { 218 public void propertyChange(PropertyChangeEvent e) { 219 String propertyName = e.getPropertyName(); 220 AbstractButton button = (AbstractButton )getTarget(); 221 if (button == null) { Action action = (Action )e.getSource(); 223 action.removePropertyChangeListener(this); 224 } else { 225 if (propertyName.equals(Action.NAME)) { 226 String text = (String ) e.getNewValue(); 227 button.setText(text); 228 button.repaint(); 229 } else if (propertyName.equals(Action.SHORT_DESCRIPTION)) { 230 String text = (String ) e.getNewValue(); 231 button.setToolTipText(text); 232 } else if (propertyName.equals("enabled")) { 233 Boolean enabledState = (Boolean ) e.getNewValue(); 234 button.setEnabled(enabledState.booleanValue()); 235 button.repaint(); 236 } else if (propertyName.equals(Action.ACTION_COMMAND_KEY)) { 237 button.setActionCommand((String )e.getNewValue()); 238 } 239 } 240 } 241 }; 242 } 243 244 248 private void writeObject(ObjectOutputStream s) throws IOException { 249 s.defaultWriteObject(); 250 if (getUIClassID().equals(uiClassID)) { 251 byte count = JComponent.getWriteObjCounter(this); 252 JComponent.setWriteObjCounter(this, --count); 253 if (count == 0 && ui != null) { 254 ui.installUI(this); 255 } 256 } 257 } 258 259 260 269 protected String paramString() { 270 return super.paramString(); 271 } 272 273 274 278 279 291 public AccessibleContext getAccessibleContext() { 292 if (accessibleContext == null) { 293 accessibleContext = new AccessibleJRadioButton(); 294 } 295 return accessibleContext; 296 } 297 298 313 protected class AccessibleJRadioButton extends AccessibleJToggleButton { 314 315 321 public AccessibleRole getAccessibleRole() { 322 return AccessibleRole.RADIO_BUTTON; 323 } 324 325 } } 327 328 | Popular Tags |