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");
}
});