Hi all

I have 4 jRadioButton in a buttonGroup1 now I want to know
how to determine which radio button is selected within these 4 radio button on Button Click event..
I tried to but failed.. here is my code

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        
        str1=buttonGroup1.getSelection().getActionCommand();
        System.out.println(str1);
    }

I am getting null value from this code. Why can't figure?????:-/

Thanking you all in advance

Has nothing to do with the fact that your buttons are in a ButtonGroup or not

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
   Object o = evt.getSource();
   if(o == button1) {
    ...
   }
   else if(o == button2) {
    ...

Has nothing to do with the fact that your buttons are in a ButtonGroup or not

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
   Object o = evt.getSource();
   if(o == button1) {
    ...
   }
   else if(o == button2) {
    ...

Sorry If I am not clear on my point - I have 1 jbutton 4 jradioButton if I click on jbutton after selecting jradioButton then which jradioButton is selected how can I know. I think the code you gave will reply me about the jbutton, or you want to tell me to apply the above code on jradioButton....

... although there's no reason why your code shouldn't work; this does:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;

public class RadioButtons implements ActionListener {

   public static void main(String[] args) {
      new RadioButtons();
   }

   JFrame frame = new JFrame("Radio Buttons");
   JRadioButton b1 = new JRadioButton("Button1");
   JRadioButton b2 = new JRadioButton("Button2");
   ButtonGroup group = new ButtonGroup();

   RadioButtons() {
      frame.setLayout(new FlowLayout());

      frame.add(b1);
      frame.add(b2);

      group.add(b1);
      group.add(b2);

      b1.setActionCommand("Action1");
      b2.setActionCommand("Action2");

      b1.addActionListener(this);
      b2.addActionListener(this);

      frame.pack();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }

   @Override
   public void actionPerformed(ActionEvent arg0) {
      String a = group.getSelection().getActionCommand();
      System.out.println(a);
   }

}

an afterthought - were you relying on the default action name (button's text)?
There's a pretty neat gotcha there. When you specify an action name it's stored in the model, not the button. If you query the model (eg via getSelection()) when you haven't explicitly set an action name you get null. The default of using the button's text is implemented at the button level - the button's getActionCommand() queries the model, and it that returns null, it uses the button's text. Hence:

JRadioButton b = new JRadioButton("B");
b.getActionCommand()
returns "B"
b.getModel.getActionCommand() returns null
group.getSelection() returns the selected button's model (why oh why???)
group.getSelection().getActionCommand(); returns null.
but with an additional
b.setActionCommand("B");
it all works beautifully

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.