1. /*
  2. * @(#)CheckboxGroup.java 1.36 04/05/18
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt;
  8. /**
  9. * The <code>CheckboxGroup</code> class is used to group together
  10. * a set of <code>Checkbox</code> buttons.
  11. * <p>
  12. * Exactly one check box button in a <code>CheckboxGroup</code> can
  13. * be in the "on" state at any given time. Pushing any
  14. * button sets its state to "on" and forces any other button that
  15. * is in the "on" state into the "off" state.
  16. * <p>
  17. * The following code example produces a new check box group,
  18. * with three check boxes:
  19. * <p>
  20. * <hr><blockquote><pre>
  21. * setLayout(new GridLayout(3, 1));
  22. * CheckboxGroup cbg = new CheckboxGroup();
  23. * add(new Checkbox("one", cbg, true));
  24. * add(new Checkbox("two", cbg, false));
  25. * add(new Checkbox("three", cbg, false));
  26. * </pre></blockquote><hr>
  27. * <p>
  28. * This image depicts the check box group created by this example:
  29. * <p>
  30. * <img src="doc-files/CheckboxGroup-1.gif"
  31. * alt="Shows three checkboxes, arranged vertically, labeled one, two, and three. Checkbox one is in the on state."
  32. * ALIGN=center HSPACE=10 VSPACE=7>
  33. * <p>
  34. * @version 1.36 05/18/04
  35. * @author Sami Shaio
  36. * @see java.awt.Checkbox
  37. * @since JDK1.0
  38. */
  39. public class CheckboxGroup implements java.io.Serializable {
  40. /**
  41. * The current choice.
  42. * @serial
  43. * @see #getCurrent()
  44. * @see #setCurrent(Checkbox)
  45. */
  46. Checkbox selectedCheckbox = null;
  47. /*
  48. * JDK 1.1 serialVersionUID
  49. */
  50. private static final long serialVersionUID = 3729780091441768983L;
  51. /**
  52. * Creates a new instance of <code>CheckboxGroup</code>.
  53. */
  54. public CheckboxGroup() {
  55. }
  56. /**
  57. * Gets the current choice from this check box group.
  58. * The current choice is the check box in this
  59. * group that is currently in the "on" state,
  60. * or <code>null</code> if all check boxes in the
  61. * group are off.
  62. * @return the check box that is currently in the
  63. * "on" state, or <code>null</code>.
  64. * @see java.awt.Checkbox
  65. * @see java.awt.CheckboxGroup#setSelectedCheckbox
  66. * @since JDK1.1
  67. */
  68. public Checkbox getSelectedCheckbox() {
  69. return getCurrent();
  70. }
  71. /**
  72. * @deprecated As of JDK version 1.1,
  73. * replaced by <code>getSelectedCheckbox()</code>.
  74. */
  75. @Deprecated
  76. public Checkbox getCurrent() {
  77. return selectedCheckbox;
  78. }
  79. /**
  80. * Sets the currently selected check box in this group
  81. * to be the specified check box.
  82. * This method sets the state of that check box to "on" and
  83. * sets all other check boxes in the group to be off.
  84. * <p>
  85. * If the check box argument is <tt>null</tt>, all check boxes
  86. * in this check box group are deselected. If the check box argument
  87. * belongs to a different check box group, this method does
  88. * nothing.
  89. * @param box the <code>Checkbox</code> to set as the
  90. * current selection.
  91. * @see java.awt.Checkbox
  92. * @see java.awt.CheckboxGroup#getSelectedCheckbox
  93. * @since JDK1.1
  94. */
  95. public void setSelectedCheckbox(Checkbox box) {
  96. setCurrent(box);
  97. }
  98. /**
  99. * @deprecated As of JDK version 1.1,
  100. * replaced by <code>setSelectedCheckbox(Checkbox)</code>.
  101. */
  102. @Deprecated
  103. public synchronized void setCurrent(Checkbox box) {
  104. if (box != null && box.group != this) {
  105. return;
  106. }
  107. Checkbox oldChoice = this.selectedCheckbox;
  108. this.selectedCheckbox = box;
  109. if (oldChoice != null && oldChoice != box && oldChoice.group == this) {
  110. oldChoice.setState(false);
  111. }
  112. if (box != null && oldChoice != box && !box.getState()) {
  113. box.setStateInternal(true);
  114. }
  115. }
  116. /**
  117. * Returns a string representation of this check box group,
  118. * including the value of its current selection.
  119. * @return a string representation of this check box group.
  120. */
  121. public String toString() {
  122. return getClass().getName() + "[selectedCheckbox=" + selectedCheckbox + "]";
  123. }
  124. }