0N/A/*
2362N/A * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage javax.swing;
0N/A
0N/Aimport java.awt.*;
0N/Aimport java.awt.event.*;
0N/Aimport java.beans.*;
0N/A
0N/Aimport javax.swing.plaf.*;
0N/Aimport javax.accessibility.*;
0N/A
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.IOException;
0N/A
0N/A
0N/A/**
0N/A * An implementation of a check box -- an item that can be selected or
0N/A * deselected, and which displays its state to the user.
0N/A * By convention, any number of check boxes in a group can be selected.
0N/A * See <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a>
0N/A * in <em>The Java Tutorial</em>
0N/A * for examples and information on using check boxes.
0N/A * <p>
0N/A * Buttons can be configured, and to some degree controlled, by
0N/A * <code><a href="Action.html">Action</a></code>s. Using an
0N/A * <code>Action</code> with a button has many benefits beyond directly
0N/A * configuring a button. Refer to <a href="Action.html#buttonActions">
0N/A * Swing Components Supporting <code>Action</code></a> for more
0N/A * details, and you can find more information in <a
0N/A * href="http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html">How
0N/A * to Use Actions</a>, a section in <em>The Java Tutorial</em>.
0N/A * <p>
0N/A * <strong>Warning:</strong> Swing is not thread safe. For more
0N/A * information see <a
0N/A * href="package-summary.html#threading">Swing's Threading
0N/A * Policy</a>.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A *
0N/A * @see JRadioButton
0N/A *
0N/A * @beaninfo
0N/A * attribute: isContainer false
0N/A * description: A component which can be selected or deselected.
0N/A *
0N/A * @author Jeff Dinkins
0N/A */
0N/Apublic class JCheckBox extends JToggleButton implements Accessible {
0N/A
0N/A /** Identifies a change to the flat property. */
0N/A public static final String BORDER_PAINTED_FLAT_CHANGED_PROPERTY = "borderPaintedFlat";
0N/A
0N/A private boolean flat = false;
0N/A
0N/A /**
0N/A * @see #getUIClassID
0N/A * @see #readObject
0N/A */
0N/A private static final String uiClassID = "CheckBoxUI";
0N/A
0N/A
0N/A /**
0N/A * Creates an initially unselected check box button with no text, no icon.
0N/A */
0N/A public JCheckBox () {
0N/A this(null, null, false);
0N/A }
0N/A
0N/A /**
0N/A * Creates an initially unselected check box with an icon.
0N/A *
0N/A * @param icon the Icon image to display
0N/A */
0N/A public JCheckBox(Icon icon) {
0N/A this(null, icon, false);
0N/A }
0N/A
0N/A /**
0N/A * Creates a check box with an icon and specifies whether
0N/A * or not it is initially selected.
0N/A *
0N/A * @param icon the Icon image to display
0N/A * @param selected a boolean value indicating the initial selection
0N/A * state. If <code>true</code> the check box is selected
0N/A */
0N/A public JCheckBox(Icon icon, boolean selected) {
0N/A this(null, icon, selected);
0N/A }
0N/A
0N/A /**
0N/A * Creates an initially unselected check box with text.
0N/A *
0N/A * @param text the text of the check box.
0N/A */
0N/A public JCheckBox (String text) {
0N/A this(text, null, false);
0N/A }
0N/A
0N/A /**
0N/A * Creates a check box where properties are taken from the
0N/A * Action supplied.
0N/A *
0N/A * @since 1.3
0N/A */
0N/A public JCheckBox(Action a) {
0N/A this();
0N/A setAction(a);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Creates a check box with text and specifies whether
0N/A * or not it is initially selected.
0N/A *
0N/A * @param text the text of the check box.
0N/A * @param selected a boolean value indicating the initial selection
0N/A * state. If <code>true</code> the check box is selected
0N/A */
0N/A public JCheckBox (String text, boolean selected) {
0N/A this(text, null, selected);
0N/A }
0N/A
0N/A /**
0N/A * Creates an initially unselected check box with
0N/A * the specified text and icon.
0N/A *
0N/A * @param text the text of the check box.
0N/A * @param icon the Icon image to display
0N/A */
0N/A public JCheckBox(String text, Icon icon) {
0N/A this(text, icon, false);
0N/A }
0N/A
0N/A /**
0N/A * Creates a check box with text and icon,
0N/A * and specifies whether or not it is initially selected.
0N/A *
0N/A * @param text the text of the check box.
0N/A * @param icon the Icon image to display
0N/A * @param selected a boolean value indicating the initial selection
0N/A * state. If <code>true</code> the check box is selected
0N/A */
0N/A public JCheckBox (String text, Icon icon, boolean selected) {
0N/A super(text, icon, selected);
0N/A setUIProperty("borderPainted", Boolean.FALSE);
0N/A setHorizontalAlignment(LEADING);
0N/A }
0N/A
0N/A /**
0N/A * Sets the <code>borderPaintedFlat</code> property,
0N/A * which gives a hint to the look and feel as to the
0N/A * appearance of the check box border.
0N/A * This is usually set to <code>true</code> when a
0N/A * <code>JCheckBox</code> instance is used as a
0N/A * renderer in a component such as a <code>JTable</code> or
0N/A * <code>JTree</code>. The default value for the
0N/A * <code>borderPaintedFlat</code> property is <code>false</code>.
0N/A * This method fires a property changed event.
0N/A * Some look and feels might not implement flat borders;
0N/A * they will ignore this property.
0N/A *
0N/A * @param b <code>true</code> requests that the border be painted flat;
0N/A * <code>false</code> requests normal borders
0N/A * @see #isBorderPaintedFlat
0N/A * @beaninfo
0N/A * bound: true
0N/A * attribute: visualUpdate true
0N/A * description: Whether the border is painted flat.
0N/A * @since 1.3
0N/A */
0N/A public void setBorderPaintedFlat(boolean b) {
0N/A boolean oldValue = flat;
0N/A flat = b;
0N/A firePropertyChange(BORDER_PAINTED_FLAT_CHANGED_PROPERTY, oldValue, flat);
0N/A if (b != oldValue) {
0N/A revalidate();
0N/A repaint();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Gets the value of the <code>borderPaintedFlat</code> property.
0N/A *
0N/A * @return the value of the <code>borderPaintedFlat</code> property
0N/A * @see #setBorderPaintedFlat
0N/A * @since 1.3
0N/A */
0N/A public boolean isBorderPaintedFlat() {
0N/A return flat;
0N/A }
0N/A
0N/A /**
0N/A * Resets the UI property to a value from the current look and feel.
0N/A *
0N/A * @see JComponent#updateUI
0N/A */
0N/A public void updateUI() {
0N/A setUI((ButtonUI)UIManager.getUI(this));
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns a string that specifies the name of the L&F class
0N/A * that renders this component.
0N/A *
0N/A * @return the string "CheckBoxUI"
0N/A * @see JComponent#getUIClassID
0N/A * @see UIDefaults#getUI
0N/A * @beaninfo
0N/A * expert: true
0N/A * description: A string that specifies the name of the L&F class
0N/A */
0N/A public String getUIClassID() {
0N/A return uiClassID;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * The icon for checkboxs comes from the look and feel,
0N/A * not the Action; this is overriden to do nothing.
0N/A */
0N/A void setIconFromAction(Action a) {
0N/A }
0N/A
0N/A /*
0N/A * See readObject and writeObject in JComponent for more
0N/A * information about serialization in Swing.
0N/A */
0N/A private void writeObject(ObjectOutputStream s) throws IOException {
0N/A s.defaultWriteObject();
0N/A if (getUIClassID().equals(uiClassID)) {
0N/A byte count = JComponent.getWriteObjCounter(this);
0N/A JComponent.setWriteObjCounter(this, --count);
0N/A if (count == 0 && ui != null) {
0N/A ui.installUI(this);
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * See JComponent.readObject() for information about serialization
0N/A * in Swing.
0N/A */
0N/A private void readObject(ObjectInputStream s)
0N/A throws IOException, ClassNotFoundException
0N/A {
0N/A s.defaultReadObject();
0N/A if (getUIClassID().equals(uiClassID)) {
0N/A updateUI();
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns a string representation of this JCheckBox. This method
0N/A * is intended to be used only for debugging purposes, and the
0N/A * content and format of the returned string may vary between
0N/A * implementations. The returned string may be empty but may not
0N/A * be <code>null</code>.
0N/A * specific new aspects of the JFC components.
0N/A *
0N/A * @return a string representation of this JCheckBox.
0N/A */
0N/A protected String paramString() {
0N/A return super.paramString();
0N/A }
0N/A
0N/A/////////////////
0N/A// Accessibility support
0N/A////////////////
0N/A
0N/A /**
0N/A * Gets the AccessibleContext associated with this JCheckBox.
0N/A * For JCheckBoxes, the AccessibleContext takes the form of an
0N/A * AccessibleJCheckBox.
0N/A * A new AccessibleJCheckBox instance is created if necessary.
0N/A *
0N/A * @return an AccessibleJCheckBox that serves as the
0N/A * AccessibleContext of this JCheckBox
0N/A * @beaninfo
0N/A * expert: true
0N/A * description: The AccessibleContext associated with this CheckBox.
0N/A */
0N/A public AccessibleContext getAccessibleContext() {
0N/A if (accessibleContext == null) {
0N/A accessibleContext = new AccessibleJCheckBox();
0N/A }
0N/A return accessibleContext;
0N/A }
0N/A
0N/A /**
0N/A * This class implements accessibility support for the
0N/A * <code>JCheckBox</code> class. It provides an implementation of the
0N/A * Java Accessibility API appropriate to check box user-interface
0N/A * elements.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A */
0N/A protected class AccessibleJCheckBox extends AccessibleJToggleButton {
0N/A
0N/A /**
0N/A * Get the role of this object.
0N/A *
0N/A * @return an instance of AccessibleRole describing the role of the object
0N/A * @see AccessibleRole
0N/A */
0N/A public AccessibleRole getAccessibleRole() {
0N/A return AccessibleRole.CHECK_BOX;
0N/A }
0N/A
0N/A } // inner class AccessibleJCheckBox
0N/A}