1. /*
  2. * @(#)CheckboxGroup.java 1.27 01/11/29
  3. *
  4. * Copyright 2002 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. * ALIGN=center HSPACE=10 VSPACE=7>
  32. * <p>
  33. * @version 1.27 11/29/01
  34. * @author Sami Shaio
  35. * @see java.awt.Checkbox
  36. * @since JDK1.0
  37. */
  38. public class CheckboxGroup implements java.io.Serializable {
  39. /**
  40. * The current choice.
  41. * @serial
  42. * @see getCurrent()
  43. * @see setCurrent()
  44. */
  45. Checkbox selectedCheckbox = null;
  46. /*
  47. * JDK 1.1 serialVersionUID
  48. */
  49. private static final long serialVersionUID = 3729780091441768983L;
  50. /**
  51. * Creates a new instance of <code>CheckboxGroup</code>.
  52. */
  53. public CheckboxGroup() {
  54. }
  55. /**
  56. * Gets the current choice from this check box group.
  57. * The current choice is the check box in this
  58. * group that is currently in the "on" state,
  59. * or <code>null</code> if all check boxes in the
  60. * group are off.
  61. * @return the check box that is currently in the
  62. * "on" state, or <code>null</code>.
  63. * @see java.awt.Checkbox
  64. * @see java.awt.CheckboxGroup#setSelectedCheckbox
  65. * @since JDK1.1
  66. */
  67. public Checkbox getSelectedCheckbox() {
  68. return getCurrent();
  69. }
  70. /**
  71. * @deprecated As of JDK version 1.1,
  72. * replaced by <code>getSelectedCheckbox()</code>.
  73. */
  74. public Checkbox getCurrent() {
  75. return selectedCheckbox;
  76. }
  77. /**
  78. * Sets the currently selected check box in this group
  79. * to be the specified check box.
  80. * This method sets the state of that check box to "on" and
  81. * sets all other check boxes in the group to be off.
  82. * <p>
  83. * If the check box argument is <tt>null</tt>, all check boxes
  84. * in this check box group are deselected. If the check box argument
  85. * belongs to a different check box group, this method does
  86. * nothing.
  87. * @param box the <code>Checkbox</code> to set as the
  88. * current selection.
  89. * @see java.awt.Checkbox
  90. * @see java.awt.CheckboxGroup#getSelectedCheckbox
  91. * @since JDK1.1
  92. */
  93. public void setSelectedCheckbox(Checkbox box) {
  94. setCurrent(box);
  95. }
  96. /**
  97. * @deprecated As of JDK version 1.1,
  98. * replaced by <code>setSelectedCheckbox(Checkbox)</code>.
  99. */
  100. public synchronized void setCurrent(Checkbox box) {
  101. if (box != null && box.group != this) {
  102. return;
  103. }
  104. Checkbox oldChoice = this.selectedCheckbox;
  105. this.selectedCheckbox = box;
  106. if ((oldChoice != null) && (oldChoice != box)) {
  107. oldChoice.setState(false);
  108. }
  109. if (box != null && oldChoice != box && !box.getState()) {
  110. box.setStateInternal(true);
  111. }
  112. }
  113. /**
  114. * Returns a string representation of this check box group,
  115. * including the value of its current selection.
  116. * @return a string representation of this check box group.
  117. */
  118. public String toString() {
  119. return getClass().getName() + "[selectedCheckbox=" + selectedCheckbox + "]";
  120. }
  121. }