Search code examples
javastringswingjradiobuttongetselection

JRadioButton selection to String


I have a selection of JRadioButtons in a group. In this case they are contained in ButtonGroup "group".

An example of a button would be reviewrequest = new JRadioButton("request for review"); etc.

I am trying to convert the selected item into a string with: categorystring = group.getSelection().toString(); (For sake of the example we will be selecting the button above)

Whenever I use this method however, instead of getting what I would like the (reviewrequest) or even ("request for review") I get something like this for a new String value:

javax.swing.JToggleButton$ToggleButtonModel@482fdd28

Any ideas? Thanks!


Solution

  • The problem is that the ButtonGroup#getSelection() is returning the ButtonModel for the JRadioButton that is now selected, and you're seeing the public String toString() returned from this ButtonModel object, and this information is not terribly useful.

    For your code to work, you will want to set the actionCommand of the JRadioButtons by calling .setActionCommand(...) passing in the String of interest, and then getting this from the ButtonModel:

    ButtonModel model = group.getSelection();
    
    // always first check that something is indeed selected
    if (model != null) {
        categorystring = model.getActionCommand();
    }
    

    For example:

    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class ButtonModelExample extends JPanel {
        private static final String[] BUTTON_TEXTS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
        private ButtonGroup buttonGroup = new ButtonGroup();
        private JTextField resultField = new JTextField(10);
        
        public ButtonModelExample() {
            JPanel topPanel = new JPanel();
            topPanel.add(new JLabel("Result:"));
            topPanel.add(resultField);
            resultField.setFocusable(false);
            
            JPanel radioPanel = new JPanel(new GridLayout(0, 1));
            for (String buttonText : BUTTON_TEXTS) {
                JRadioButton radioBtn = new JRadioButton(buttonText);
                radioBtn.setActionCommand(buttonText);
                radioBtn.addChangeListener(cl -> {
                    ButtonModel buttonModel = buttonGroup.getSelection();
                    if (buttonModel != null) {
                        String text = buttonModel.getActionCommand();
                        resultField.setText(text);
                    }
                });
                radioPanel.add(radioBtn);
                buttonGroup.add(radioBtn);
            }
            
            setLayout(new BorderLayout());
            add(topPanel, BorderLayout.PAGE_START);
            add(radioPanel);
        }
        
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> {
                ButtonModelExample mainPanel = new ButtonModelExample();
    
                JFrame frame = new JFrame("GUI");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(mainPanel);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            });
        }
    
    }