Current Position:Home > Finding out which JRadioButton in a ButtonGroup is selected

Finding out which JRadioButton in a ButtonGroup is selected

Update:11-30Source: network consolidation
Advertisement
Why hasn't a simple solution for this problem been included in Java to date?
Granted, enumerating through the radio buttons, or simply checking each one's isSelected() status with a series of if/else if statements, isn't that tough. But wouldn't it be a whole lot easier to provide a method in ButtonGroup that returns a reference to the selected radio button? Am I missing some important reason why this functionality should not be part of ButtonGroup?
Thanks.

The Best Answer

Advertisement
something like this seems to work OK
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Testing extends JFrame
  JRadioButton[] rb = new JRadioButton[4];
  ButtonGroup group = new ButtonGroup();
  public Testing()
    setLocation(400,300);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JPanel jp = new JPanel(new GridLayout(4,1));
    for(int x = 0; x < rb.length; x++)
      rb[x] = new JRadioButton("RB "+x);
      rb[x].setActionCommand(""+x);
      group.add(rb[x]);
      jp.add(rb[x]);
    JButton btn = new JButton("OK");
    getContentPane().add(jp,BorderLayout.CENTER);
    getContentPane().add(btn,BorderLayout.SOUTH);
    pack();
    rb[0].setSelected(true);
    btn.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent ae){
        JOptionPane.showMessageDialog(getContentPane(),"Selected index = "+group.getSelection().getActionCommand());}});
  public static void main(String[] args){new Testing().setVisible(true);}
}
  • Finding out which JRadioButton in a ButtonGroup is selected Update:11-30

    Why hasn't a simple solution for this problem been included in Java to date? Granted, enumerating through the radio buttons, or simply checking each one's isSelected() status with a series of if/else if statements, isn't that tough. But wouldn't it b

  • ButtonGroup multiple selection not possible. Update:11-30

    Hey Guys, I have been trying to add a Buttongroup where i can select more than 1 JCheckBox. does anyone know how i can modify the code below so that i can select more than just one JCheckBox in the group? ButtonGroup GroupPreferences = new ButtonGrou

  • Java.lang.NullPointerException with JRadioButton Update:10-11

    Hi, Well, I'm using JRadioButton for the first time. So far, I haven't had much luck. I'm trying to check whether one of the radio buttons I have in a ButtonGroup are selected. I have one of the two radiobuttons selected from the get-go. The followin

  • How to set a particular JRadioButtion which is in a ButtonGroup? Update:11-30

    My problem is that I have two JRadioButton is a ButtonGroup. When I click on one Button, a message is sent out, and a message is received from the other side. Depending on the message received, I want to set one Button to be selected and the other on

  • How can I check which JRadioButton is selected in whichTabbedPane? Update:11-30

    Hello, I have several TabbedPanes with a mass of JRadioButtons in one ButtonGroup. Now I search an easy way how I can find out which RadioButton is selected in which TabbedPane. With "getParent()" I can get a lot but not e.g. the name of the Tab

  • JRadioButton.setSelected(false) doesn't work? Update:11-30

    Hey there. I usually answer questions in this forum but this time I have a question myself. I'm trying to de-select JRadioButtons in a ButtonGroup but it's not working. I've searched the forum but didn't find anything. Here's the very simple code: im

  • JRadioButton gets selected automatically Update:11-30

    Hello friends, There is something funny happening in an application I have been maintaining. There are three panels with JRadioButtons within a ButtonGroup class. When I do a particular operation, exactly speaking 'Backspacing through a textfield' th

  • How can i make any of the choices in a JRadioButtons not selectable? Update:11-30

    How can i make any of the choices in a JRadioButtons not selectable? The user cannot select any of them.. I have made logical relationship between JRadioButtons by the ButtonGroup.. thanks..setEnabled( false );Read other 2 answers

  • JRadioButton in front of JPanel Update:11-30

    Hi, I want to make a JRadioButton (in a ButtonGroup) which contains, instead of only a JLable behind it, a JPanel with some textfields ... I tried to just add the component to the radioButton, but it overwrites the selection round... Any ideas ? I kn

  • Arrays if ButtonGroups yields an error: Update:11-30

    I need to create a series of radiobuttons based on a two dimensional array the size of which is defined at runtime. A value is passed into a function that creates an array of radiobuttons of equal dimensions (Square) 2x2 or 3x3 or 4x4 etc. each row o