KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > ButtonGroup


1 /*
2    SwingWT
3    Copyright(c)2003-2004, Tomer Bartletz
4  
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7  
8    Contact me by electronic mail: tomerb@users.sourceforge.net
9
10    $Log: ButtonGroup.java,v $
11    Revision 1.2 2003/12/14 09:13:38 bobintetley
12    Added CVS log to source headers
13
14  */

15 package swingwtx.swing;
16
17 import java.util.*;
18
19 /**
20  * This class is used to create a multiple-exclusion scope for
21  * a set of buttons. Creating a set of buttons with the
22  * same <code>ButtonGroup</code> object means that
23  * turning "on" one of those buttons
24  * turns off all other buttons in the group.
25  *
26  * @author Tomer Barletz, tomerb@users.sourceforge.net
27  * @version
28  */

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