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