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 47 public class JCheckBox extends JToggleButton implements Accessible { 48 49 50 public static final String BORDER_PAINTED_FLAT_CHANGED_PROPERTY = "borderPaintedFlat"; 51 52 private boolean flat = false; 53 54 58 private static final String uiClassID = "CheckBoxUI"; 59 60 61 64 public JCheckBox () { 65 this(null, null, false); 66 } 67 68 73 public JCheckBox(Icon icon) { 74 this(null, icon, false); 75 } 76 77 85 public JCheckBox(Icon icon, boolean selected) { 86 this(null, icon, selected); 87 } 88 89 94 public JCheckBox (String text) { 95 this(text, null, false); 96 } 97 98 104 public JCheckBox(Action a) { 105 this(); 106 setAction(a); 107 } 108 109 110 118 public JCheckBox (String text, boolean selected) { 119 this(text, null, selected); 120 } 121 122 129 public JCheckBox(String text, Icon icon) { 130 this(text, icon, false); 131 } 132 133 142 public JCheckBox (String text, Icon icon, boolean selected) { 143 super(text, icon, selected); 144 setUIProperty("borderPainted", Boolean.FALSE); 145 setHorizontalAlignment(LEADING); 146 } 147 148 169 public void setBorderPaintedFlat(boolean b) { 170 boolean oldValue = flat; 171 flat = b; 172 firePropertyChange(BORDER_PAINTED_FLAT_CHANGED_PROPERTY, oldValue, flat); 173 if (b != oldValue) { 174 revalidate(); 175 repaint(); 176 } 177 } 178 179 185 public boolean isBorderPaintedFlat() { 186 return flat; 187 } 188 189 194 public void updateUI() { 195 setUI((ButtonUI)UIManager.getUI(this)); 196 } 197 198 199 210 public String getUIClassID() { 211 return uiClassID; 212 } 213 214 215 227 protected void configurePropertiesFromAction(Action a) { 228 String [] types = { Action.MNEMONIC_KEY, Action.NAME, 229 Action.SHORT_DESCRIPTION, 230 Action.ACTION_COMMAND_KEY, "enabled" }; 231 configurePropertiesFromAction(a, types); 232 } 233 234 251 protected PropertyChangeListener createActionPropertyChangeListener(Action a) { 252 return new AbstractActionPropertyChangeListener (this, a) { 253 public void propertyChange(PropertyChangeEvent e) { 254 String propertyName = e.getPropertyName(); 255 AbstractButton button = (AbstractButton )getTarget(); 256 if (button == null) { Action action = (Action )e.getSource(); 258 action.removePropertyChangeListener(this); 259 } else { 260 if (propertyName.equals(Action.NAME)) { 261 String text = (String ) e.getNewValue(); 262 button.setText(text); 263 button.repaint(); 264 } else if (propertyName.equals(Action.SHORT_DESCRIPTION)) { 265 String text = (String ) e.getNewValue(); 266 button.setToolTipText(text); 267 } else if (propertyName.equals("enabled")) { 268 Boolean enabledState = (Boolean ) e.getNewValue(); 269 button.setEnabled(enabledState.booleanValue()); 270 button.repaint(); 271 } else if (propertyName.equals(Action.ACTION_COMMAND_KEY)) { 272 button.setActionCommand((String )e.getNewValue()); 273 } 274 } 275 } 276 }; 277 } 278 279 280 284 private void writeObject(ObjectOutputStream s) throws IOException { 285 s.defaultWriteObject(); 286 if (getUIClassID().equals(uiClassID)) { 287 byte count = JComponent.getWriteObjCounter(this); 288 JComponent.setWriteObjCounter(this, --count); 289 if (count == 0 && ui != null) { 290 ui.installUI(this); 291 } 292 } 293 } 294 295 296 300 private void readObject(ObjectInputStream s) 301 throws IOException , ClassNotFoundException 302 { 303 s.defaultReadObject(); 304 if (getUIClassID().equals(uiClassID)) { 305 updateUI(); 306 } 307 } 308 309 310 320 protected String paramString() { 321 return super.paramString(); 322 } 323 324 328 340 public AccessibleContext getAccessibleContext() { 341 if (accessibleContext == null) { 342 accessibleContext = new AccessibleJCheckBox(); 343 } 344 return accessibleContext; 345 } 346 347 362 protected class AccessibleJCheckBox extends AccessibleJToggleButton { 363 364 370 public AccessibleRole getAccessibleRole() { 371 return AccessibleRole.CHECK_BOX; 372 } 373 374 } } 376 377 | Popular Tags |