1. /*
  2. * @(#)CheckboxGroup.java 1.29 00/02/02
  3. *
  4. * Copyright 1995-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.awt;
  11. /**
  12. * The <code>CheckboxGroup</code> class is used to group together
  13. * a set of <code>Checkbox</code> buttons.
  14. * <p>
  15. * Exactly one check box button in a <code>CheckboxGroup</code> can
  16. * be in the "on" state at any given time. Pushing any
  17. * button sets its state to "on" and forces any other button that
  18. * is in the "on" state into the "off" state.
  19. * <p>
  20. * The following code example produces a new check box group,
  21. * with three check boxes:
  22. * <p>
  23. * <hr><blockquote><pre>
  24. * setLayout(new GridLayout(3, 1));
  25. * CheckboxGroup cbg = new CheckboxGroup();
  26. * add(new Checkbox("one", cbg, true));
  27. * add(new Checkbox("two", cbg, false));
  28. * add(new Checkbox("three", cbg, false));
  29. * </pre></blockquote><hr>
  30. * <p>
  31. * This image depicts the check box group created by this example:
  32. * <p>
  33. * <img src="doc-files/CheckboxGroup-1.gif"
  34. * ALIGN=center HSPACE=10 VSPACE=7>
  35. * <p>
  36. * @version 1.29 02/02/00
  37. * @author Sami Shaio
  38. * @see java.awt.Checkbox
  39. * @since JDK1.0
  40. */
  41. public class CheckboxGroup implements java.io.Serializable {
  42. /**
  43. * The current choice.
  44. * @serial
  45. * @see getCurrent()
  46. * @see setCurrent()
  47. */
  48. Checkbox selectedCheckbox = null;
  49. /*
  50. * JDK 1.1 serialVersionUID
  51. */
  52. private static final long serialVersionUID = 3729780091441768983L;
  53. /**
  54. * Creates a new instance of <code>CheckboxGroup</code>.
  55. */
  56. public CheckboxGroup() {
  57. }
  58. /**
  59. * Gets the current choice from this check box group.
  60. * The current choice is the check box in this
  61. * group that is currently in the "on" state,
  62. * or <code>null</code> if all check boxes in the
  63. * group are off.
  64. * @return the check box that is currently in the
  65. * "on" state, or <code>null</code>.
  66. * @see java.awt.Checkbox
  67. * @see java.awt.CheckboxGroup#setSelectedCheckbox
  68. * @since JDK1.1
  69. */
  70. public Checkbox getSelectedCheckbox() {
  71. return getCurrent();
  72. }
  73. /**
  74. * @deprecated As of JDK version 1.1,
  75. * replaced by <code>getSelectedCheckbox()</code>.
  76. */
  77. public Checkbox getCurrent() {
  78. return selectedCheckbox;
  79. }
  80. /**
  81. * Sets the currently selected check box in this group
  82. * to be the specified check box.
  83. * This method sets the state of that check box to "on" and
  84. * sets all other check boxes in the group to be off.
  85. * <p>
  86. * If the check box argument is <tt>null</tt>, all check boxes
  87. * in this check box group are deselected. If the check box argument
  88. * belongs to a different check box group, this method does
  89. * nothing.
  90. * @param box the <code>Checkbox</code> to set as the
  91. * current selection.
  92. * @see java.awt.Checkbox
  93. * @see java.awt.CheckboxGroup#getSelectedCheckbox
  94. * @since JDK1.1
  95. */
  96. public void setSelectedCheckbox(Checkbox box) {
  97. setCurrent(box);
  98. }
  99. /**
  100. * @deprecated As of JDK version 1.1,
  101. * replaced by <code>setSelectedCheckbox(Checkbox)</code>.
  102. */
  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)) {
  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. }