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