You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.2 KiB
89 lines
2.2 KiB
package de.memtext.buttons; |
|
|
|
import java.util.Enumeration; |
|
|
|
import javax.swing.ButtonGroup; |
|
import javax.swing.ButtonModel; |
|
import javax.swing.JLabel; |
|
import javax.swing.JPanel; |
|
import javax.swing.JRadioButton; |
|
|
|
public class ButtonGroupPanel extends JPanel { |
|
ButtonGroup group = new ButtonGroup(); |
|
ButtonWithValue hiddenNoSelectionButton = new ButtonWithValue("", null); |
|
|
|
public ButtonGroupPanel() { |
|
this(null); |
|
} |
|
public ButtonGroupPanel(String header) { |
|
super(); |
|
if (header != null) |
|
this.add(new JLabel(header)); |
|
|
|
hiddenNoSelectionButton.setVisible(false); |
|
hiddenNoSelectionButton.setSelected(true); |
|
group.add(hiddenNoSelectionButton); |
|
} |
|
public void addCategory(String label, Object value,boolean isSelected) { |
|
ButtonWithValue b = new ButtonWithValue(label, value); |
|
this.add(b); |
|
group.add(b); |
|
b.setSelected(isSelected); |
|
} |
|
public void addCategory(String label, Object value) { |
|
addCategory(label, value,false); |
|
} |
|
|
|
public void addCategory(String label, int i) { |
|
addCategory(label, new Integer(i)); |
|
} |
|
public void addCategory(String label, int i,boolean isSelected) { |
|
addCategory(label, new Integer(i),isSelected); |
|
} |
|
/** |
|
* If you pass null as an argument no element will be selected |
|
* @param label |
|
*/ |
|
public void setSelected(String label) { |
|
if (label == null) |
|
hiddenNoSelectionButton.setSelected(true); |
|
else |
|
for (Enumeration en = group.getElements(); |
|
en.hasMoreElements(); |
|
) { |
|
ButtonWithValue b = (ButtonWithValue) en.nextElement(); |
|
if (b.getText().equals(label)) { |
|
b.doClick(); |
|
break; |
|
} |
|
} |
|
} |
|
|
|
public Object getSelectedValue() { |
|
ButtonWithValue selectedButton=null; |
|
for (Enumeration en = group.getElements(); |
|
en.hasMoreElements(); |
|
) { |
|
ButtonWithValue b = (ButtonWithValue) en.nextElement(); |
|
if (b.isSelected()) { |
|
selectedButton=b; |
|
break; |
|
} |
|
} |
|
return selectedButton.getValue(); |
|
} |
|
class ButtonWithValue extends JRadioButton { |
|
private Object value; |
|
ButtonWithValue(String label, Object value) { |
|
super(label); |
|
this.value = value; |
|
} |
|
Object getValue() { |
|
return value; |
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
//Created on 17.02.2004 at 13:40:06
|