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.
rkamarowskiAsked:
Who is Participating?
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.