1. /*
  2. * @(#)ButtonGroup.java 1.37 04/05/05
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing;
  8. import java.awt.event.*;
  9. import java.util.Vector;
  10. import java.util.Enumeration;
  11. import java.io.Serializable;
  12. /**
  13. * This class is used to create a multiple-exclusion scope for
  14. * a set of buttons. Creating a set of buttons with the
  15. * same <code>ButtonGroup</code> object means that
  16. * turning "on" one of those buttons
  17. * turns off all other buttons in the group.
  18. * <p>
  19. * A <code>ButtonGroup</code> can be used with
  20. * any set of objects that inherit from <code>AbstractButton</code>.
  21. * Typically a button group contains instances of
  22. * <code>JRadioButton</code>,
  23. * <code>JRadioButtonMenuItem</code>,
  24. * or <code>JToggleButton</code>.
  25. * It wouldn't make sense to put an instance of
  26. * <code>JButton</code> or <code>JMenuItem</code>
  27. * in a button group
  28. * because <code>JButton</code> and <code>JMenuItem</code>
  29. * don't implement the selected state.
  30. * <p>
  31. * Initially, all buttons in the group are unselected. Once any button is
  32. * selected, one button is always selected in the group. There is no way
  33. * to turn a button programmatically to "off", in order to clear the button
  34. * group. To give the appearance of "none selected", add an invisible radio
  35. * button to the group and then programmatically select that button to
  36. * turn off all the displayed radio buttons. For example, a normal button
  37. * with the label "none" could be wired to select the invisible radio button.
  38. * <p>
  39. * For examples and further information on using button groups see
  40. * <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/button.html#radiobutton">How to Use Radio Buttons</a>,
  41. * a section in <em>The Java Tutorial</em>.
  42. * <p>
  43. * <strong>Warning:</strong>
  44. * Serialized objects of this class will not be compatible with
  45. * future Swing releases. The current serialization support is
  46. * appropriate for short term storage or RMI between applications running
  47. * the same version of Swing. As of 1.4, support for long term storage
  48. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  49. * has been added to the <code>java.beans</code> package.
  50. * Please see {@link java.beans.XMLEncoder}.
  51. *
  52. * @version 1.37 05/05/04
  53. * @author Jeff Dinkins
  54. */
  55. public class ButtonGroup implements Serializable {
  56. // the list of buttons participating in this group
  57. protected Vector<AbstractButton> buttons = new Vector();
  58. /**
  59. * The current selection.
  60. */
  61. ButtonModel selection = null;
  62. /**
  63. * Creates a new <code>ButtonGroup</code>.
  64. */
  65. public ButtonGroup() {}
  66. /**
  67. * Adds the button to the group.
  68. * @param b the button to be added
  69. */
  70. public void add(AbstractButton b) {
  71. if(b == null) {
  72. return;
  73. }
  74. buttons.addElement(b);
  75. if (b.isSelected()) {
  76. if (selection == null) {
  77. selection = b.getModel();
  78. } else {
  79. b.setSelected(false);
  80. }
  81. }
  82. b.getModel().setGroup(this);
  83. }
  84. /**
  85. * Removes the button from the group.
  86. * @param b the button to be removed
  87. */
  88. public void remove(AbstractButton b) {
  89. if(b == null) {
  90. return;
  91. }
  92. buttons.removeElement(b);
  93. if(b.getModel() == selection) {
  94. selection = null;
  95. }
  96. b.getModel().setGroup(null);
  97. }
  98. /**
  99. * Returns all the buttons that are participating in
  100. * this group.
  101. * @return an <code>Enumeration</code> of the buttons in this group
  102. */
  103. public Enumeration<AbstractButton> getElements() {
  104. return buttons.elements();
  105. }
  106. /**
  107. * Returns the model of the selected button.
  108. * @return the selected button model
  109. */
  110. public ButtonModel getSelection() {
  111. return selection;
  112. }
  113. /**
  114. * Sets the selected value for the <code>ButtonModel</code>.
  115. * Only one button in the group may be selected at a time.
  116. * @param m the <code>ButtonModel</code>
  117. * @param b <code>true</code> if this button is to be
  118. * selected, otherwise <code>false</code>
  119. */
  120. public void setSelected(ButtonModel m, boolean b) {
  121. if (b && m != null && m != selection) {
  122. ButtonModel oldSelection = selection;
  123. selection = m;
  124. if (oldSelection != null) {
  125. oldSelection.setSelected(false);
  126. }
  127. m.setSelected(true);
  128. }
  129. }
  130. /**
  131. * Returns whether a <code>ButtonModel</code> is selected.
  132. * @return <code>true</code> if the button is selected,
  133. * otherwise returns <code>false</code>
  134. */
  135. public boolean isSelected(ButtonModel m) {
  136. return (m == selection);
  137. }
  138. /**
  139. * Returns the number of buttons in the group.
  140. * @return the button count
  141. * @since 1.3
  142. */
  143. public int getButtonCount() {
  144. if (buttons == null) {
  145. return 0;
  146. } else {
  147. return buttons.size();
  148. }
  149. }
  150. }