• Post Reply Bookmark Topic Watch Topic
  • New Topic

grabbing selected radio button

 
Donald Hume
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey JavaRanch,

I am playing around with a gui interface and had some questions on capturing a radio button value once a submit button is pressed.

I have the actionEvent set up for the submit button, just not sure what code to have execute once it is pressed.

I've done the following:
1)Created radio buttons jusing JRadioButton butt1 = new JRadioButton(); for five buttons.

2)Created a button group ButtonGroup group = new ButtonGroup(); and added the radio buttons group.add(butt1); etc.

The java docs section on radio buttons is a bit convoluted and im not sure exactly what some of the commands do.. such as the following:



I think setMnemonic sets the "action" key or w/e -- basically like a hot key that selects that button.

Not exactly sure what setActionCommand does. I've tried using this following to grab the selected button -- w/ errors as follow:



So here are my two questions.

1) How do I grab which button is selected?
2) What does the statement return? do I need to set some sort of other value? I'm familiar with javascript setting a name="" value that will get returned.

I appreciate the help and support you guys give here. Hope I was clear enough in describing my problem.

Don
 
Campbell Ritchie
Marshal
Posts: 52543
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Last time I tried anything like that I had to get an Enumeration of Buttons from the ButtonGroup.getElements() method and iterate through it to find the selectedButton.

You can add ActionListeners to radio buttons, that might be a good way to implement your functionality.

Sounds more like a Swing topic, so I shall move you . . .
 
G Estes
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say you are playing around with some GUI interface (btw - that is redundant), what GUI are you working with? Swing, or JSP?

If Swing, adding an action listener for any event you want to occur on the selection is one way (if you want the selection immediate). If you are setting state across several things, then pressing an "Enter" button, then you can do a button.isSelected() to tell what is set and not set.

However, that said, your statement of a "Submit" button leads me to think you are not doing a Swing app, but a web app (i.e. JSP, etc...), so that would be a different implementation...
[ August 13, 2008: Message edited by: Gil Estes ]
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Donald Hume:

I've done the following:
1)Created radio buttons jusing JRadioButton butt1 = new JRadioButton(); for five buttons.

2)Created a button group ButtonGroup group = new ButtonGroup(); and added the radio buttons group.add(butt1); etc.

The java docs section on radio buttons is a bit convoluted and im not sure exactly what some of the commands do.. such as the following:

begButton.setActionCommand("Beginner");

Not exactly sure what setActionCommand does.


If you set an action command string, then it will be used when
the button fires events [see ActionEvent.getActionCommand()]
and it can be used to identify a button or its ButtonModel.


I've tried using this following to grab the selected button -- w/ errors as follow:



So here are my two questions.

1) How do I grab which button is selected?
2) What does the statement return?


group.getSelection() returns the model of the selected button,
not the button itself (nor a String, which seems to be what your
code is expecting). If you really want a string you could do
group.getSelection().getActionCommand() but:

(a) It will throw an NPE if no button is selected.

(b) It will return null if you haven't set an action command.

[note: yourButton.getActionCommand() will return your
button's text if no action command has been set, but
yourButton.getModel().getActionCommand() will not.]
[ August 25, 2008: Message edited by: Brian Cole ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!