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!
The problem is that the
ButtonGroup#getSelection()is returning the ButtonModel for the JRadioButton that is now selected, and you're seeing thepublic 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:For example: