Renato Dinhani
Renato Dinhani

Reputation: 36696

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

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;
}

Upvotes: 2

Views: 5521

Answers (6)

Mia Clarke
Mia Clarke

Reputation: 8204

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.

Upvotes: 1

camickr
camickr

Reputation: 324118

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

Upvotes: 2

Anthony Accioly
Anthony Accioly

Reputation: 22471

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.

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

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();
         }
      });
   }
}

Upvotes: 4

Jack
Jack

Reputation: 133587

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..

Upvotes: 3

duffymo
duffymo

Reputation: 308763

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);

    }
}

Upvotes: 1

Related Questions