Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating an exam application where I created different frames for each question. The user can select the corresponding radio button for the correct answer. I have given 'previous' and 'next' button to look for the questions. Here's where the problem is. When a user selects a radio button, he moves to the next question using the 'next' button. But, when he presses the 'previous' button, all the radio buttons are shown unselected.

What I have tried:

I came to know about the button group and I tried it so that only one radio button can be selected. But I want the radio button selected by the user, to remain selected even when the user comes back to that question. I tried the following code.
Java
if(jRadioButton1.isSelected()==true)
       {
           jRadioButton1.setSelected(true);
           jRadioButton2.setSelected(false);
           jRadioButton3.setSelected(false);
           jRadioButton4.setSelected(false);
       }
       else if(jRadioButton2.isSelected()==true)
       {
           jRadioButton2.setSelected(true);
           jRadioButton1.setSelected(false);
           jRadioButton3.setSelected(false);
           jRadioButton4.setSelected(false);
       }
       else if(jRadioButton3.isSelected()==true)
       {
           jRadioButton3.setSelected(true);
           jRadioButton2.setSelected(false);
           jRadioButton1.setSelected(false);
           jRadioButton4.setSelected(false);
       }
       else if(jRadioButton4.isSelected()==true)
       {
           jRadioButton4.setSelected(true);
           jRadioButton2.setSelected(false);
           jRadioButton3.setSelected(false);
           jRadioButton1.setSelected(false);
       }


But it didn't work. Could anyone provide a solution for this?
Posted
Updated 27-Jul-18 21:06pm
v2
Comments
Richard MacCutchan 28-Jul-18 4:59am    
"But it didn't work"
It is amazing how many people post questions with the above as the only piece of information, expecting us to magically guess what it was that didn't work, and exactly what problems occurred.

Please edit your question and provide exact details of what happens when you run your code.
@k5hu 28-Jul-18 14:05pm    
The radio button once selected gets deselected when moved to the next frame. The above code was meant to work but even though I added 'setSelected(true)' method it didn't work.
[no name] 28-Jul-18 14:15pm    
Your code doesn't make sense; a "group" of radio buttons "toggle" themselves.

(Maybe you need a "group box"; or switch to .NET)

What about "bindings"? Are the rb's bound to "properties" in order to transfer / maintain "state"?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900