1 15 package swingwtx.swing; 16 17 import java.util.*; 18 19 29 public class ButtonGroup { 30 31 protected Vector buttons = new Vector(); 32 ButtonModel selection = null; 33 34 public ButtonGroup() {} 35 36 public void add(AbstractButton b) { 37 buttons.add(b); 38 if (b.isSelected()) { 39 if (selection == null) { 40 selection = b.getModel(); 41 } else { 42 b.setSelected(false); 43 } 44 } 45 b.getModel().setGroup(this); 46 } 47 48 public void remove(AbstractButton b) { 49 if(b == null) { 50 return; 51 } 52 buttons.remove(b); 53 if(b.getModel() == selection) { 54 selection = null; 55 } 56 b.getModel().setGroup(null); 57 } 58 59 public Enumeration getElements() { 60 return buttons.elements(); 61 } 62 63 public ButtonModel getSelection() { 64 return selection; 65 } 66 67 public void setSelected(ButtonModel m, boolean b) { 68 if (b && m != null && m != selection) { 69 ButtonModel oldSelection = selection; 70 selection = m; 71 if (oldSelection != null) { 72 oldSelection.setSelected(false); 73 } 74 m.setSelected(true); 75 } 76 } 77 78 public boolean isSelected(ButtonModel m) { 79 return (m == selection); 80 } 81 82 public int getButtonCount() { 83 if (buttons == null) { 84 return 0; 85 } else { 86 return buttons.size(); 87 } 88 } 89 90 } 91 | Popular Tags |