Whenever I chose any radioButton it automatically selects the last one, What should I do?

Viewed 52

Below is a short snippet of my code used in my Swing Application, Its an mcq application where I've used radioButtons as the mean to select the chosen option, However when I try selecting any option from 1-4, It automatically selects the last one. Now I've tried putting the last button in the else if condition as well but I dont know what I should write in the else condition then.

JButton btnNext_1 = new JButton("Next");
btnNext_1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    /*  if(db_ans.equals(studentAnswer))
        {
            
            tmarks=tmarks+db_marks;
            System.out.println("correct-second");
            
        }*/ 
            
            buttonGroup.clearSelection();
                if(radioButton.isSelected())
                {
                    studentAnswer=radioButton.getText();
                    radioButton_1.setSelected(false);
                    radioButton_2.setSelected(false);
                    radioButton_3.setSelected(false);
                    System.out.println(studentAnswer);
                    
                }
                else if(radioButton_1.isSelected())
                {
                    studentAnswer=radioButton_1.getText();
                    System.out.println(studentAnswer);
                    radioButton.setSelected(false);
                    radioButton_2.setSelected(false);
                    radioButton_3.setSelected(false);
                    
                }
                else if(radioButton_2.isSelected())
                {
                    studentAnswer=radioButton_2.getText();
                    System.out.println(studentAnswer);
                radioButton.setSelected(false);
                radioButton_1.setSelected(false);
                radioButton_3.setSelected(false);
                }
                else if  {
                    studentAnswer=radioButton_3.getText();
                    System.out.println(studentAnswer);
                    radioButton.setSelected(false);
                    radioButton_1.setSelected(false);
                    radioButton_2.setSelected(false);
                }
                
                if(db_ans.equals(studentAnswer))
                {
                    
                    tmarks=tmarks+db_marks;
                    System.out.println("correct-second");
                }

});

2 Answers

As said in comments you would want to use a ButtonGroup this will allow only 1 JRadioButton to be selected at a time, see below for a simple example:

enter image description here

TestApp.java:

import java.awt.event.ActionEvent;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;

public class TestApp {

    public TestApp() {
        initComponents();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(TestApp::new);
    }

    private void initComponents() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        JRadioButton option1 = new JRadioButton("Option 1");
        option1.setSelected(true);
        JRadioButton option2 = new JRadioButton("Option 2");

        ButtonGroup group = new ButtonGroup();
        group.add(option1);
        group.add(option2);

        panel.add(option1);
        panel.add(option2);

        JButton button = new JButton("What did I choose?");
        button.addActionListener((ActionEvent e) -> {
            if (option1.isSelected()) {
                JOptionPane.showMessageDialog(frame, "You chose option 1");
            } else if (option2.isSelected()) {
                JOptionPane.showMessageDialog(frame, "You chose option 2");
            }
        });
        panel.add(button);

        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

}

Essentially each JRadioButton is added to the same ButtonGroup and each JRadioButton is then added to the panel. Now you will only be able to select 1 option from within the same group, and there is no need to set any other JRadioButton in the same group to false.

you have all radio button selected so android is last one read means radioButton_2 this read last so radioButton2 selected

Related