KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > JXRadioGroup


1 /*
2  * $Id: JXRadioGroup.java,v 1.1 2004/07/28 21:21:11 aim Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing;
9
10 import java.awt.Color JavaDoc;
11 import java.awt.Component JavaDoc;
12
13 import java.awt.event.ActionEvent JavaDoc;
14 import java.awt.event.ActionListener JavaDoc;
15
16 import java.util.ArrayList JavaDoc;
17
18 import javax.swing.AbstractButton JavaDoc;
19 import javax.swing.BoxLayout JavaDoc;
20 import javax.swing.ButtonGroup JavaDoc;
21 import javax.swing.ButtonModel JavaDoc;
22 import javax.swing.JPanel JavaDoc;
23 import javax.swing.JRadioButton JavaDoc;
24
25 /**
26  * @author Amy Fowler
27  * @version 1.0
28  */

29
30 public class JXRadioGroup extends JPanel JavaDoc {
31     private ButtonGroup JavaDoc buttonGroup;
32     private ArrayList JavaDoc values = new ArrayList JavaDoc();
33     private ActionListener JavaDoc actionHandler;
34     private ArrayList JavaDoc actionListeners;
35
36     public JXRadioGroup() {
37         setLayout(new BoxLayout JavaDoc(this, BoxLayout.X_AXIS));
38         buttonGroup = new ButtonGroup JavaDoc();
39     }
40
41     public JXRadioGroup(Object JavaDoc radioValues[]) {
42         this();
43         for(int i = 0; i < radioValues.length; i++) {
44             add(radioValues[i]);
45         }
46     }
47
48     public void add(Object JavaDoc radioValue) {
49         values.add(radioValue);
50         addButton(new JRadioButton JavaDoc(radioValue.toString()));
51     }
52
53     public void add(AbstractButton JavaDoc button) {
54         values.add(button.getText());
55         addButton(button);
56     }
57
58     private void addButton(AbstractButton JavaDoc button) {
59         buttonGroup.add(button);
60         super.add(button);
61         if (actionHandler == null) {
62             actionHandler = new ActionListener JavaDoc() {
63                 public void actionPerformed(ActionEvent JavaDoc e) {
64                     fireActionEvent(e);
65                 }
66             };
67         }
68         button.addActionListener(actionHandler);
69     }
70
71     public AbstractButton JavaDoc getSelectedButton() {
72         ButtonModel JavaDoc selectedModel = buttonGroup.getSelection();
73         Component JavaDoc children[] = getComponents();
74         for(int i = 0; i < children.length; i++) {
75             AbstractButton JavaDoc button = (AbstractButton JavaDoc)children[i];
76             if (button.getModel() == selectedModel) {
77                 return button;
78             }
79         }
80         return null;
81     }
82
83     private int getSelectedIndex() {
84         ButtonModel JavaDoc selectedModel = buttonGroup.getSelection();
85         Component JavaDoc children[] = getComponents();
86         for (int i = 0; i < children.length; i++) {
87             AbstractButton JavaDoc button = (AbstractButton JavaDoc) children[i];
88             if (button.getModel() == selectedModel) {
89                 return i;
90             }
91         }
92         return -1;
93     }
94
95     public Object JavaDoc getSelectedValue() {
96         int index = getSelectedIndex();
97         return values.get(index);
98     }
99
100     public void setSelectedValue(Object JavaDoc value) {
101         int index = values.indexOf(value);
102         AbstractButton JavaDoc button = (AbstractButton JavaDoc)getComponent(index);
103         button.setSelected(true);
104     }
105
106     public void addActionListener(ActionListener JavaDoc l) {
107         if (actionListeners == null) {
108             actionListeners = new ArrayList JavaDoc();
109         }
110         actionListeners.add(l);
111     }
112
113     public void removeActionListener(ActionListener JavaDoc l) {
114         if (actionListeners != null) {
115             actionListeners.remove(l);
116         }
117     }
118
119     public ActionListener JavaDoc[] getActionListeners() {
120         if (actionListeners != null) {
121             return (ActionListener JavaDoc[])actionListeners.toArray(new ActionListener JavaDoc[0]);
122         }
123         return new ActionListener JavaDoc[0];
124     }
125
126     protected void fireActionEvent(ActionEvent JavaDoc e) {
127         if (actionListeners != null) {
128             for (int i = 0; i < actionListeners.size(); i++) {
129                 ActionListener JavaDoc l = (ActionListener JavaDoc) actionListeners.get(i);
130                 l.actionPerformed(e);
131             }
132         }
133     }
134 }
Popular Tags