What is the best way to determine what JRadioButton is selected?

依然范特西╮ 提交于 2019-12-24 02:35:31

问题


Currently I'm getting the selected button in this way, but I don't if this is the right/best method. MAybe there something more easy or object oriented than this.

private int getFilterType(JRadioButton... buttons) {
    for (int i = 0, n = buttons.length; i < n; i++) {
        if (buttons[i].isSelected()) {
            return i + 1;
        }
    }
    return buttons.length + 1;
}

回答1:


I like using the ButtonGroup itself for this. i.e.,

import java.awt.GridLayout;
import java.awt.event.ActionEvent;

import javax.swing.*;

public class ButtonModelEg {
   public static final String[] BUTTON_TEXTS = {"Fe", "Fi", "Fo", "Fum"};

   private static void createAndShowUI() {
      final ButtonGroup btnGroup = new ButtonGroup();

      JPanel panel = new JPanel(new GridLayout(0, 1));
      for (String btnText : BUTTON_TEXTS) {
         JRadioButton radioBtn = new JRadioButton(btnText);
         radioBtn.setActionCommand(btnText);
         btnGroup.add(radioBtn);
         panel.add(radioBtn);
      }

      final JTextField selectionField = new JTextField();

      JButton button = new JButton(new AbstractAction("Get Choice"){
         public void actionPerformed(ActionEvent arg0) {
            // get the button model selected from the button group
            ButtonModel selectedModel = btnGroup.getSelection();
            if (selectedModel != null) {
               // and dislay it
               selectionField.setText(selectedModel.getActionCommand());
            }
         }
      });

      panel.add(button);
      panel.add(selectionField);

      JFrame frame = new JFrame("ButtonModelEg");
      frame.getContentPane().add(panel);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}



回答2:


What you can do is to use a ButtonGroup (doc here) to which you add all the JRadioButton. The group will take care of managing the mutual exclusion of the button as well as providing you the getSelection() method which returns the underlying model of the radio button pressed. Then you can find an easy way to distinguish between the various models (maybe you can retrieve the parent of the model or use the setActionCommand and getActionCommand like in:

JRadioButton button1 = new JRadioButton(..);
button1.getModel().setActionCommand("MY_ACTION");

and then you can easily understand which one it is..




回答3:


You can use Darryl's Select Button Group which extend ButtonGroup to give you easy access to the radio button.




回答4:


If you have to know which button was pushed, I'd say it's best to give it an individual listener. No worries then.

public class ListenerDemo
{
    private JButton button1;
    private JButton button2;

    public ListenerDemo(ActionListener listener1, ActionListener listener2)
    {
        this.button1 = new JButton("Button 1 Label");
        this.button.addActionListener(listener1);
        this.button2 = new JButton("Button 2 Label");
        this.button.addActionListener(listener2);

    }
}



回答5:


Use ButtonGroup. The good thing about using the button group is that it takes care of things such as togglig between radio buttons for you, so that you don't have to worry about it.

You add your buttons to the group like so:

JRadioButton carrots = new JRadioButton("Carrots");
carrots.setActionCommand("carrots");

JRadioButton peas = new JRadioButton("Peas");
peas.setActionCommand("peas");

ButtonGroup group = new ButtonGroup();
group.add(carrots);
group.add(peas);

and when you need to get the selected button, you just use

group.getSelection();

You can also add listeners to each button, as shown in this example.




回答6:


I don't know if there is a way to get rid of the for loop. But at least you can use the ButtonGroup to dump the var-args. See an example here.



来源:https://stackoverflow.com/questions/6088798/what-is-the-best-way-to-determine-what-jradiobutton-is-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!