1 7 package javax.swing; 8 9 import java.awt.event.*; 10 import java.util.Vector ; 11 import java.util.Enumeration ; 12 import java.io.Serializable ; 13 14 57 public class ButtonGroup implements Serializable { 58 59 protected Vector <AbstractButton > buttons = new Vector (); 61 62 65 ButtonModel selection = null; 66 67 70 public ButtonGroup() {} 71 72 76 public void add(AbstractButton b) { 77 if(b == null) { 78 return; 79 } 80 buttons.addElement(b); 81 82 if (b.isSelected()) { 83 if (selection == null) { 84 selection = b.getModel(); 85 } else { 86 b.setSelected(false); 87 } 88 } 89 90 b.getModel().setGroup(this); 91 } 92 93 97 public void remove(AbstractButton b) { 98 if(b == null) { 99 return; 100 } 101 buttons.removeElement(b); 102 if(b.getModel() == selection) { 103 selection = null; 104 } 105 b.getModel().setGroup(null); 106 } 107 108 113 public Enumeration <AbstractButton > getElements() { 114 return buttons.elements(); 115 } 116 117 121 public ButtonModel getSelection() { 122 return selection; 123 } 124 125 132 public void setSelected(ButtonModel m, boolean b) { 133 if (b && m != null && m != selection) { 134 ButtonModel oldSelection = selection; 135 selection = m; 136 if (oldSelection != null) { 137 oldSelection.setSelected(false); 138 } 139 m.setSelected(true); 140 } 141 } 142 143 148 public boolean isSelected(ButtonModel m) { 149 return (m == selection); 150 } 151 152 157 public int getButtonCount() { 158 if (buttons == null) { 159 return 0; 160 } else { 161 return buttons.size(); 162 } 163 } 164 165 } 166 | Popular Tags |