Question

I'm preparing a test page in java and and questions are coming from database but when user choose a radio button and click finish button, cant see his score.

So program should compare radio button's text and right option field which come from database.(Just last question is working good because i wrote it myself. its not from database.)

I think i have problem on radio button item listener class but i couldnt find solution.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

public class ExamSystem extends JFrame {

    public JLabel q1,q2;

    public JButton ok;

    public JRadioButton a1,a2,a3,a4,b1,b2,b3,b4;

    public JPanel pnl1,pnl2,pnlchoices1,pnlchoices2;

    public ButtonGroup grp1,grp2,grp3,grp4,grp5;

    public int score=0;

    public Connection con;

    public Statement st;

    public ResultSet rs;

    public ExamSystem () {
        super("Exam");
        Container container = getContentPane();
        container.setLayout(new GridLayout(11,1));

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

        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            System.out.print("sürücü yüklendi");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/etest", "root", "1234");
            st = con.createStatement();

            String sql = "Select * from questions";
            rs = st.executeQuery(sql);


            //int id_col = rs.getInt("sid");
            //String id = Integer.toString(id_col);
            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");

                q1.setText(soru);
                a1.setText(birinci);
                a2.setText(ikinci);
                a3.setText(ucuncu);
                a4.setText(dorduncu);

                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);
            }
        }
        catch (Exception s)
        {
            System.out.print(s.getMessage());
        } 

        pnl2 = new JPanel();
        pnl2.setLayout(new GridLayout(1,1));

        q2 = new JLabel(" Which one is biggest?");
        pnl2.add(q2);

        pnlchoices2 = new JPanel();
        pnlchoices2.setLayout(new GridLayout(2,2));    
        b1 = new JRadioButton(" 1");
        b2 = new JRadioButton(" 2");
        b3 = new JRadioButton(" 3");
        b4 = new JRadioButton(" 4");
        pnlchoices2.add(b1);
        pnlchoices2.add(b2);
        pnlchoices2.add(b3);
        pnlchoices2.add(b4);

        container.add(pnl2);
        container.add(pnlchoices2);

        ok = new JButton("Finish");
        ok.setBackground(Color.RED);
        container.add(ok);

        setSize(500,500);
        setVisible(true);

        RadioButtonHandler handler = new RadioButtonHandler();
        a1.addItemListener(handler);
        a2.addItemListener(handler);
        a3.addItemListener(handler);
        a4.addItemListener(handler);
        b1.addItemListener(handler);
        b2.addItemListener(handler);
        b3.addItemListener(handler);
        b4.addItemListener(handler);

        grp2 = new ButtonGroup();
        grp2.add(b1);
        grp2.add(b2);
        grp2.add(b3);
        grp2.add(b4);

        ButtonHandler btnHandler = new ButtonHandler();
        ok.addActionListener(btnHandler);
    }

    public static void main(String[]args) {
        ExamSystem application = new ExamSystem();
        application.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public class RadioButtonHandler implements ItemListener {
        public void itemStateChanged(ItemEvent event) {
            if (event.getSource()==b4) 
                score++;
        }   
    }

    public class ButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            if (event.getSource()==ok) {
                JOptionPane.showMessageDialog(null,"Score: " +score,"",JOptionPane.INFORMATION_MESSAGE);
                //dispose();
            }
        }
    }
}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top