ButtonGroup - getting radiobutton text

Posted on 2006-04-21
Last Modified: 2010-03-31
I have the following code:

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

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

Question by:rkamarowski
    LVL 86

    Accepted Solution

    String command = radioButtonGroup.getSelected().getActionCommand();
    LVL 14

    Assisted Solution

    LVL 2

    Expert Comment

    Add listener to the button:

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now