There are a number of problems with your code, but the most significant problem, and the one that means the exam system isn't calculating the total score, is that you only increment the score when the last radio button changes state. You can select correct or incorrect answers for the other questions, but as your button handler only recognises the radio button b4
, this is the only radio button that will affect the score.
(Incidentally, you also increment the score when the 4
radio button is deselected, so you can make the score go as high as you like by repeatedly clicking the 4
and 2
radio buttons, for example.)
In order to fix it, there are a number of changes I would recommend making.
Firstly, I wouldn't recommend trying to keep track of the score as you go along. Instead, when you click the Finish button, run through a list of 'correct' buttons and see how many of them are checked (use the isSelected()
method for this). Add a field named correctButtons
, and declare it to be of type List<JRadioButton>
, i.e.
private List<JRadioButton> correctButtons;
We'll populate this list before the while
loop, but before we can add to the list, we need a list to add to. Add the following line above the loop:
correctButtons = new ArrayList<JRadioButton>();
You will also need to add the lines
import java.util.List;
import java.util.ArrayList;
at the top of your file.
There is also an issue with your code generating a blank set of radio buttons. This is because you create one set of radio buttons before the while
loop and once each time through the loop. Inside this loop, you set the labels of the last set of radio buttons and create a new set. Of course, the last set of buttons will never have any labels set. It would be better to only create the sets of radio buttons as you need them, inside the loop. Get rid of all this code from outside the while
loop:
pnl1 = new JPanel();
pnl1.setLayout(new GridLayout(1,1));
q1 = new JLabel();
// ... rest of code omitted, until
grp1.add(a3);
grp1.add(a4);
Inside the while
loop, move the calls to q1.setText()
and so on further down, after the labels and radio buttons have been created.
At the end of the loop, we will need to figure out which answer was correct and add the corresponding button to our list of correct buttons.
The while
loop should then look like the following:
while ( rs.next()) {
String soru = rs.getString("question");
String birinci = rs.getString("first");
String ikinci = rs.getString("second");
String ucuncu = rs.getString("third");
String dorduncu = rs.getString("forth");
String dogru = rs.getString("right");
// Calls to q1.setText, a1.setText, etc. moved from here.
pnl1 = new JPanel();
pnl1.setLayout(new GridLayout(1,1));
q1 = new JLabel();
pnl1.add(q1);
pnlchoices1 = new JPanel();
pnlchoices1.setLayout(new GridLayout(2,2));
a1 = new JRadioButton();
a2 = new JRadioButton();
a3 = new JRadioButton();
a4 = new JRadioButton();
pnlchoices1.add(a1);
pnlchoices1.add(a2);
pnlchoices1.add(a3);
pnlchoices1.add(a4);
container.add(pnl1);
container.add(pnlchoices1);
grp1 = new ButtonGroup();
grp1.add(a1);
grp1.add(a2);
grp1.add(a3);
grp1.add(a4);
// Calls to q1.setText etc moved here.
q1.setText(soru);
a1.setText(birinci);
a2.setText(ikinci);
a3.setText(ucuncu);
a4.setText(dorduncu);
// Figure out which button is for the correct answer
// and add it to our list of correct buttons.
if (dogru.equals(birinci)) {
correctButtons.add(a1);
} else if (dogru.equals(ikinci)) {
correctButtons.add(a2);
} else if (dogru.equals(ucuncu)) {
correctButtons.add(a3);
} else if (dogru.equals(dorduncu)) {
correctButtons.add(a4);
} else {
// If we get here, the correct answer is not one of the
// options. I don't know how you want to handle this.
}
}
As the radio button b4
is for another correct answer, you may want to add that to correctButtons
as well.
Next, get rid of the class RadioButtonHandler
and all of the calls to addItemListener
that use it. We don't need it any more.
Finally, modify the ButtonHandler
class's actionPerformed method
, to calculate the score using the following code. Add it immediately before the call to JOptionPane.showMessageDialog
:
int score = 0;
for (JRadioButton correctButton : correctButtons) {
if (correctButton.isSelected()) {
++score;
}
}
I made these changes to your code, and it worked as I expected it to.