1. /*
  2. * @(#)ButtonGroup.java 1.23 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 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 ButtonGroup object means that turning "on" one of those buttons
  16. * turns off all other buttons in the group. A ButtonGroup can be used with
  17. * sets of JButton, JRadioButton, or JRadioButtonMenuItem objects.
  18. * <p>
  19. * Initially, all buttons in the group are unselected. Once any button is
  20. * selected, one button is always selected in the group. There is no way
  21. * to turn a button programmatically to "off", in order to clear the button
  22. * group. To give the appearance of "none selected", add an invisible radio
  23. * button to the group and then programmatically select that button to
  24. * turn off all the displayed radio buttons. For example, a normal button
  25. * with the label "none" could be wired to select the invisible radio button.
  26. * <p>
  27. * <strong>Warning:</strong>
  28. * Serialized objects of this class will not be compatible with
  29. * future Swing releases. The current serialization support is appropriate
  30. * for short term storage or RMI between applications running the same
  31. * version of Swing. A future release of Swing will provide support for
  32. * long term persistence.
  33. *
  34. * @version 1.23 11/29/01
  35. * @author Jeff Dinkins
  36. */
  37. public class ButtonGroup implements Serializable {
  38. // the list of buttons participating in this group
  39. protected Vector buttons = new Vector();
  40. /**
  41. * The current choice.
  42. */
  43. ButtonModel selection = null;
  44. /**
  45. * Creates a new ButtonGroup.
  46. */
  47. public ButtonGroup() {}
  48. /**
  49. * Adds the button to the group.
  50. */
  51. public void add(AbstractButton b) {
  52. if(b == null) {
  53. return;
  54. }
  55. buttons.addElement(b);
  56. if(selection == null && b.isSelected()) {
  57. selection = b.getModel();
  58. }
  59. b.getModel().setGroup(this);
  60. }
  61. /**
  62. * Removes the button from the group.
  63. */
  64. public void remove(AbstractButton b) {
  65. if(b == null) {
  66. return;
  67. }
  68. buttons.removeElement(b);
  69. if(b.getModel() == selection) {
  70. selection = null;
  71. }
  72. b.getModel().setGroup(null);
  73. }
  74. /**
  75. * Return all the buttons that are participating in
  76. * this group.
  77. */
  78. public Enumeration getElements() {
  79. return buttons.elements();
  80. }
  81. /**
  82. * Return the selected button model.
  83. */
  84. public ButtonModel getSelection() {
  85. return selection;
  86. }
  87. /**
  88. * Sets the selected value for the button.
  89. */
  90. public void setSelected(ButtonModel m, boolean b) {
  91. if(b && m != selection) {
  92. ButtonModel oldSelection = selection;
  93. selection = m;
  94. if(oldSelection != null) {
  95. oldSelection.setSelected(false);
  96. }
  97. }
  98. }
  99. /**
  100. * Returns the selected value for the button.
  101. */
  102. public boolean isSelected(ButtonModel m) {
  103. return (m == selection);
  104. }
  105. }