I have a swing application that includes radio buttons on a form. I have the ButtonGroup
, however, looking at the available methods, I can't seem to get the name of the selected JRadioButton
. Here's what I can tell so far:
我有一个swing应用程序,其中包括窗体上的单选按钮。我有ButtonGroup,但是,查看可用的方法,我似乎无法获得所选的JRadioButton的名称。以下是我目前所能讲的:
-
From ButtonGroup, I can perform a
getSelection()
to return theButtonModel
. From there, I can perform agetActionCommand
, but that doesn't seem to always work. I tried different tests and got unpredictable results.从ButtonGroup,我可以执行getSelection()来返回ButtonModel。从那里,我可以执行getActionCommand,但这似乎并不总是有效。我尝试了不同的测试,结果却出乎意料。
-
Also from
ButtonGroup
, I can get an Enumeration fromgetElements()
. However, then I would have to loop through each button just to check and see if it is the one selected.同样来自ButtonGroup,我可以从getElements()得到一个枚举。但是,我必须循环遍历每个按钮来检查是否选中了它。
Is there an easier way to find out which button has been selected? I'm programing this in Java 1.3.1 and Swing.
有没有一种更简单的方法来找出哪个按钮被选中了?我在Java 1.3.1和Swing中编写这个程序。
12 个解决方案
#1
36
I would just loop through your JRadioButtons
and call isSelected()
. If you really want to go from the ButtonGroup
you can only get to the models. You could match the models to the buttons, but then if you have access to the buttons, why not use them directly?
我将循环通过您的JRadioButtons并调用isSelected()。如果你真的想从ButtonGroup走,你只能得到模型。您可以将模型与按钮相匹配,但是如果您能够访问按钮,为什么不直接使用它们呢?
#2
65
I got similar problem and solved with this:
我遇到了类似的问题,解决了这个问题:
import java.util.Enumeration;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
public class GroupButtonUtils {
public String getSelectedButtonText(ButtonGroup buttonGroup) {
for (Enumeration<AbstractButton> buttons = buttonGroup.getElements(); buttons.hasMoreElements();) {
AbstractButton button = buttons.nextElement();
if (button.isSelected()) {
return button.getText();
}
}
return null;
}
}
It returns the text of the selected button.
它返回所选按钮的文本。
#3
22
You must add setActionCommand
to the JRadioButton
then just do:
您必须将setActionCommand添加到JRadioButton,然后只需要:
String entree = entreeGroup.getSelection().getActionCommand();
Example:
例子:
java = new JRadioButton("Java");
java.setActionCommand("Java");
c = new JRadioButton("C/C++");
c.setActionCommand("c");
System.out.println("Selected Radio Button: " +
buttonGroup.getSelection().getActionCommand());
#4
4
I suggest going straight for the model approach in Swing. After you've put the component in the panel and layout manager, don't even bother keeping a specific reference to it.
我建议在Swing中直接使用模型方法。在将组件放到面板和布局管理器之后,甚至不必为它保留一个特定的引用。
If you really want the widget, then you can test each with isSelected
, or maintain a Map<ButtonModel,JRadioButton>
.
如果您真的想要小部件,那么您可以通过isSelected测试每个部件,或者维护一个Map
#5
3
You can put and actionCommand to each radio button (string).
您可以对每个单选按钮(字符串)进行put和actionCommand。
this.jButton1.setActionCommand("dog");
this.jButton2.setActionCommand("cat");
this.jButton3.setActionCommand("bird");
Assuming they're already in a ButtonGroup (state_group in this case) you can get the selected radio button like this:
假设它们已经在ButtonGroup中(在本例中是state_group),您可以像这样得到所选的单选按钮:
String selection = this.state_group.getSelection().getActionCommand();
Hope this helps
希望这有助于
#6
2
The following code displays which JRadiobutton is selected from Buttongroup on click of a button.
It is done by looping through all JRadioButtons in a particular buttonGroup.
下面的代码显示了从Buttongroup中选择的JRadiobutton按钮。它通过在一个特定的buttonGroup中循环遍历所有jradiobutton来完成。
JRadioButton firstRadioButton=new JRadioButton("Female",true);
JRadioButton secondRadioButton=new JRadioButton("Male");
//Create a radio button group using ButtonGroup
ButtonGroup btngroup=new ButtonGroup();
btngroup.add(firstRadioButton);
btngroup.add(secondRadioButton);
//Create a button with text ( What i select )
JButton button=new JButton("What i select");
//Add action listener to created button
button.addActionListener(this);
//Get selected JRadioButton from ButtonGroup
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==button)
{
Enumeration<AbstractButton> allRadioButton=btngroup.getElements();
while(allRadioButton.hasMoreElements())
{
JRadioButton temp=(JRadioButton)allRadioButton.nextElement();
if(temp.isSelected())
{
JOptionPane.showMessageDialog(null,"You select : "+temp.getText());
}
}
}
}
#7
0
You could use getSelectedObjects() of ItemSelectable (superinterface of ButtonModel) which returns the list of selected items. In case of a radio button group it can only be one or none at all.
您可以使用getSelectedObjects()项目选择(ButtonModel的超接口)来返回所选项目的列表。如果一个单选按钮组,它只能是一个或根本没有。
#8
0
Add the radiobuttons to a button group then:
然后将radiobutton添加到按钮组:
buttonGroup.getSelection().getActionCommand
.getActionCommand buttonGroup.getSelection()
#9
0
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.JRadioButton;
import javax.swing.JToggleButton;
public class RadioButton extends JRadioButton {
public class RadioButtonModel extends JToggleButton.ToggleButtonModel {
public Object[] getSelectedObjects() {
if ( isSelected() ) {
return new Object[] { RadioButton.this };
} else {
return new Object[0];
}
}
public RadioButton getButton() { return RadioButton.this; }
}
public RadioButton() { super(); setModel(new RadioButtonModel()); }
public RadioButton(Action action) { super(action); setModel(new RadioButtonModel()); }
public RadioButton(Icon icon) { super(icon); setModel(new RadioButtonModel()); }
public RadioButton(String text) { super(text); setModel(new RadioButtonModel()); }
public RadioButton(Icon icon, boolean selected) { super(icon, selected); setModel(new RadioButtonModel()); }
public RadioButton(String text, boolean selected) { super(text, selected); setModel(new RadioButtonModel()); }
public RadioButton(String text, Icon icon) { super(text, icon); setModel(new RadioButtonModel()); }
public RadioButton(String text, Icon icon, boolean selected) { super(text, icon, selected); setModel(new RadioButtonModel()); }
public static void main(String[] args) {
RadioButton b1 = new RadioButton("A");
RadioButton b2 = new RadioButton("B");
ButtonGroup group = new ButtonGroup();
group.add(b1);
group.add(b2);
b2.setSelected(true);
RadioButtonModel model = (RadioButtonModel)group.getSelection();
System.out.println(model.getButton().getText());
}
}
#10
0
Use the isSelected()
method. It will tell you the state of your radioButton. Using it in combination with a loop(say for loop) you can find which one has been selected.
使用isSelected()方法。它会告诉你你的无线电按钮的状态。使用它与循环(比方说for循环)结合使用,您可以发现已经选择了哪一个。
#11
0
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyJRadioButton extends JFrame implements ActionListener
{
JRadioButton rb1,rb2; //components
ButtonGroup bg;
MyJRadioButton()
{
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
rb1=new JRadioButton("male");
rb2=new JRadioButton("female");
//add radio button to button group
bg=new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
//add radio buttons to frame,not button group
add(rb1);
add(rb2);
//add action listener to JRadioButton, not ButtonGroup
rb1.addActionListener(this);
rb2.addActionListener(this);
pack();
setVisible(true);
}
public static void main(String[] args)
{
new MyJRadioButton(); //calling constructor
}
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println(((JRadioButton) e.getSource()).getActionCommand());
}
}
}
#12
-3
jRadioOne = new javax.swing.JRadioButton();
jRadioTwo = new javax.swing.JRadioButton();
jRadioThree = new javax.swing.JRadioButton();
... then for every button:
…然后对于每一个按钮:
buttonGroup1.add(jRadioOne);
jRadioOne.setText("One");
jRadioOne.setActionCommand(ONE);
jRadioOne.addActionListener(radioButtonActionListener);
...listener
…侦听器
ActionListener radioButtonActionListener = new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
radioButtonActionPerformed(evt);
}
};
...do whatever you need as response to event
…作为对事件的回应,你需要做什么?
protected void radioButtonActionPerformed(ActionEvent evt) {
System.out.println(evt.getActionCommand());
}
#1
36
I would just loop through your JRadioButtons
and call isSelected()
. If you really want to go from the ButtonGroup
you can only get to the models. You could match the models to the buttons, but then if you have access to the buttons, why not use them directly?
我将循环通过您的JRadioButtons并调用isSelected()。如果你真的想从ButtonGroup走,你只能得到模型。您可以将模型与按钮相匹配,但是如果您能够访问按钮,为什么不直接使用它们呢?
#2
65
I got similar problem and solved with this:
我遇到了类似的问题,解决了这个问题:
import java.util.Enumeration;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
public class GroupButtonUtils {
public String getSelectedButtonText(ButtonGroup buttonGroup) {
for (Enumeration<AbstractButton> buttons = buttonGroup.getElements(); buttons.hasMoreElements();) {
AbstractButton button = buttons.nextElement();
if (button.isSelected()) {
return button.getText();
}
}
return null;
}
}
It returns the text of the selected button.
它返回所选按钮的文本。
#3
22
You must add setActionCommand
to the JRadioButton
then just do:
您必须将setActionCommand添加到JRadioButton,然后只需要:
String entree = entreeGroup.getSelection().getActionCommand();
Example:
例子:
java = new JRadioButton("Java");
java.setActionCommand("Java");
c = new JRadioButton("C/C++");
c.setActionCommand("c");
System.out.println("Selected Radio Button: " +
buttonGroup.getSelection().getActionCommand());
#4
4
I suggest going straight for the model approach in Swing. After you've put the component in the panel and layout manager, don't even bother keeping a specific reference to it.
我建议在Swing中直接使用模型方法。在将组件放到面板和布局管理器之后,甚至不必为它保留一个特定的引用。
If you really want the widget, then you can test each with isSelected
, or maintain a Map<ButtonModel,JRadioButton>
.
如果您真的想要小部件,那么您可以通过isSelected测试每个部件,或者维护一个Map
#5
3
You can put and actionCommand to each radio button (string).
您可以对每个单选按钮(字符串)进行put和actionCommand。
this.jButton1.setActionCommand("dog");
this.jButton2.setActionCommand("cat");
this.jButton3.setActionCommand("bird");
Assuming they're already in a ButtonGroup (state_group in this case) you can get the selected radio button like this:
假设它们已经在ButtonGroup中(在本例中是state_group),您可以像这样得到所选的单选按钮:
String selection = this.state_group.getSelection().getActionCommand();
Hope this helps
希望这有助于
#6
2
The following code displays which JRadiobutton is selected from Buttongroup on click of a button.
It is done by looping through all JRadioButtons in a particular buttonGroup.
下面的代码显示了从Buttongroup中选择的JRadiobutton按钮。它通过在一个特定的buttonGroup中循环遍历所有jradiobutton来完成。
JRadioButton firstRadioButton=new JRadioButton("Female",true);
JRadioButton secondRadioButton=new JRadioButton("Male");
//Create a radio button group using ButtonGroup
ButtonGroup btngroup=new ButtonGroup();
btngroup.add(firstRadioButton);
btngroup.add(secondRadioButton);
//Create a button with text ( What i select )
JButton button=new JButton("What i select");
//Add action listener to created button
button.addActionListener(this);
//Get selected JRadioButton from ButtonGroup
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==button)
{
Enumeration<AbstractButton> allRadioButton=btngroup.getElements();
while(allRadioButton.hasMoreElements())
{
JRadioButton temp=(JRadioButton)allRadioButton.nextElement();
if(temp.isSelected())
{
JOptionPane.showMessageDialog(null,"You select : "+temp.getText());
}
}
}
}
#7
0
You could use getSelectedObjects() of ItemSelectable (superinterface of ButtonModel) which returns the list of selected items. In case of a radio button group it can only be one or none at all.
您可以使用getSelectedObjects()项目选择(ButtonModel的超接口)来返回所选项目的列表。如果一个单选按钮组,它只能是一个或根本没有。
#8
0
Add the radiobuttons to a button group then:
然后将radiobutton添加到按钮组:
buttonGroup.getSelection().getActionCommand
.getActionCommand buttonGroup.getSelection()
#9
0
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.JRadioButton;
import javax.swing.JToggleButton;
public class RadioButton extends JRadioButton {
public class RadioButtonModel extends JToggleButton.ToggleButtonModel {
public Object[] getSelectedObjects() {
if ( isSelected() ) {
return new Object[] { RadioButton.this };
} else {
return new Object[0];
}
}
public RadioButton getButton() { return RadioButton.this; }
}
public RadioButton() { super(); setModel(new RadioButtonModel()); }
public RadioButton(Action action) { super(action); setModel(new RadioButtonModel()); }
public RadioButton(Icon icon) { super(icon); setModel(new RadioButtonModel()); }
public RadioButton(String text) { super(text); setModel(new RadioButtonModel()); }
public RadioButton(Icon icon, boolean selected) { super(icon, selected); setModel(new RadioButtonModel()); }
public RadioButton(String text, boolean selected) { super(text, selected); setModel(new RadioButtonModel()); }
public RadioButton(String text, Icon icon) { super(text, icon); setModel(new RadioButtonModel()); }
public RadioButton(String text, Icon icon, boolean selected) { super(text, icon, selected); setModel(new RadioButtonModel()); }
public static void main(String[] args) {
RadioButton b1 = new RadioButton("A");
RadioButton b2 = new RadioButton("B");
ButtonGroup group = new ButtonGroup();
group.add(b1);
group.add(b2);
b2.setSelected(true);
RadioButtonModel model = (RadioButtonModel)group.getSelection();
System.out.println(model.getButton().getText());
}
}
#10
0
Use the isSelected()
method. It will tell you the state of your radioButton. Using it in combination with a loop(say for loop) you can find which one has been selected.
使用isSelected()方法。它会告诉你你的无线电按钮的状态。使用它与循环(比方说for循环)结合使用,您可以发现已经选择了哪一个。
#11
0
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyJRadioButton extends JFrame implements ActionListener
{
JRadioButton rb1,rb2; //components
ButtonGroup bg;
MyJRadioButton()
{
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
rb1=new JRadioButton("male");
rb2=new JRadioButton("female");
//add radio button to button group
bg=new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
//add radio buttons to frame,not button group
add(rb1);
add(rb2);
//add action listener to JRadioButton, not ButtonGroup
rb1.addActionListener(this);
rb2.addActionListener(this);
pack();
setVisible(true);
}
public static void main(String[] args)
{
new MyJRadioButton(); //calling constructor
}
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println(((JRadioButton) e.getSource()).getActionCommand());
}
}
}
#12
-3
jRadioOne = new javax.swing.JRadioButton();
jRadioTwo = new javax.swing.JRadioButton();
jRadioThree = new javax.swing.JRadioButton();
... then for every button:
…然后对于每一个按钮:
buttonGroup1.add(jRadioOne);
jRadioOne.setText("One");
jRadioOne.setActionCommand(ONE);
jRadioOne.addActionListener(radioButtonActionListener);
...listener
…侦听器
ActionListener radioButtonActionListener = new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
radioButtonActionPerformed(evt);
}
};
...do whatever you need as response to event
…作为对事件的回应,你需要做什么?
protected void radioButtonActionPerformed(ActionEvent evt) {
System.out.println(evt.getActionCommand());
}