• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

ButtonGroup - getting radiobutton text

I have the following code:

                  barRadioButton = new JRadioButton("Bar", true);
                  lineRadioButton = new JRadioButton("Line", true);
                  pieRadioButton = new JRadioButton("Pie", true);
                  radioButtonGroup = new ButtonGroup();
                  radioButtonGroup.add(barRadioButton);
                  radioButtonGroup.add(lineRadioButton);
                  radioButtonGroup.add(pieRadioButton);

How can I get the 'name' of the button selected (ie, Bar, Line or Pie).

Thanks.
0
rkamarowski
Asked:
rkamarowski
2 Solutions
 
CEHJCommented:
String command = radioButtonGroup.getSelected().getActionCommand();
0
 
hoomanvCommented:
0
 
ucoolCommented:
Add listener to the button:

mybtn.addActionListener(
    new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            doMyAction();  // code to execute when button is pressed
        }
    }
);
. . .
radioButtonGroup.add(mybtn);  // add the button to a JPanel (eg, radioButtonGroup).
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now