Hello to everyone and happy new year...

I have 3 JRadioButtons in my JFrame

private JRadioButton one = new JRadioButton("one ");
	private JRadioButton two = new JRadioButton("two ");
	private JRadioButton three = new JRadioButton("three ");

And I Have add them to a group

private ButtonGroup Group = new ButtonGroup();
Group .add(one);
		Group .add(two);
		Group .add(three);
		
		JPanel myPanel = new JPanel();
		myPanel .add(one);
		myPanel .add(two);
		myPanel .add(three);

So far so good;)

Now I want to detect which radio button is selected from the group but when i am trying to use this line of code

Group.isSelected(m)

I dont know what to put for parameter m which in Javadoc says is a ButtonModel...

How I can convert my RadioButtons to ButtonModels?

Thx in advance:)

Recommended Answers

All 8 Replies

This is (IMHO) a horrible design omission in Swing - AFAIK there's no simple method to get the selected JRadioButton in a ButtonGroup. Most people just loop thru the buttons using their inherited isSelected() method to find the one (if any) that's selected OR they have a Listener for the buttons that keeps track of the current selection.

I was considering to use ActionListeners but i thought that since is available from the API why not to use it...

Ok thanks I will try to make my own class to find out which is selected

Here's some code you can use as a starting point. Please share your final solution with everyone here - it's a common problem.

public  JRadioButton getSelectedButton(ButtonGroup group) {
      Enumeration<AbstractButton> e = group.getElements();
      while (e.hasMoreElements()) {
         AbstractButton b =  e.nextElement();
        if (b.isSelected()) return (JRadioButton) b;
      }
      return null;
    }

Thanks for the code...

A small question, when i will return (JRadioButton) b

what will be returned?
The name of the JRadioButton?(if is set)?


EDIT

public  String getSelectedButton(ButtonGroup group) {
Enumeration<AbstractButton> e = group.getElements();
while (e.hasMoreElements()) {
   AbstractButton b =  e.nextElement();
   if (b.isSelected()) return  b.getText();
}
return null;
}

Well i changed the code a bit :p

Instead of returning a JRadioButton i am returning the text of the RadioButon, that will solve my problem

Thanks again

No, it will return the actual JRadioButton object. So, for example, you could say
if (getSelectedButton(group) == one) ...

ps: I just typed that code but I didn't test it, it's just a starter for you. You may need to debug it a bit. maybe.

So far there is no problem, but I use my version where I just return the text of the selected JRadioButton...

I 'ill post if anything new comes up:p

That's probably OK for a college project, but just remember that it's poor practice for real life - Java is designed for use with multiple languages, so the text in your GUI could end up in a user-defined choice of English, Arabic, or Japanese; you can't use it reliably to identify things in your code.
Anyway. mark this one as "solved" and start a new thread if you get a new roblem.

Thanks for the advise, this is a college project yes, but i will try to implement your version as well :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.