Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8980189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:01:07+00:00 2026-06-15T20:01:07+00:00

I dont know if this is a silly issue that i am having or

  • 0

I dont know if this is a silly issue that i am having or what but for the life of me i cant figure out the solution. I am writing a quiz program with 20 questions. The program asks each question and the user has 5 choices which are 5 JRadio Buttons once the user choose one answer he can hit next to go to the next question or previous to review a question. The issue that I am having is once the user answers a question and hit next the previous radio button selected stays selected, what i mean is if a answer A for question 1 and hit next choice A will be selected for question 2 and so on. the 5 radio buttons are in a button group and i used the clear selection method to clear the selection it works fine except if the user hits the previous button to review a question once he hits the next button to continue all the selections get clear, lets say the user answers 10 questions and goes back to review question number 3 once he hits next to go back up to question 10 all the questions in between will get cleared.

I will add the next and previous implementations below.
Any ideas will be much appreciated.

// implements next button

  nextBT.setPreferredSize(new Dimension(70, 30));
    nextBT.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {

            int nextQuestion = -1;
            boolean answer = getAnswer(currentQuestion, selectedButton()).equals(getCorrectAnswer(currentQuestion));
            questionHistory[currentHistoryIndex][0] = currentQuestion;
            questionHistory[currentHistoryIndex][1] = selectedButton();

            if (currentHistoryIndex == maxHistoryIndex) {

                //generate next question number to use
                int currentLevel = currentQuestion / 25;
                int nextLevel = currentLevel;
                if (answer) {
                    if (currentLevel < 3) {
                        nextLevel++;
                    }
                } else {
                    if (currentLevel > 0) {
                        nextLevel--;
                    }
                }
                while (true) {
                    int k = 0;
                    Random randomNum = new Random();
                    nextQuestion = nextLevel * 25 + (int) (randomNum.nextInt(25));
                    for (k = 0; k < maxHistoryIndex; k++) {
                        if (questionHistory[k][0] == nextQuestion) {
                            break;
                        }
                    }
                    if (k == maxHistoryIndex) {
                        break;
                    }
                }
                currentHistoryIndex++;
                maxHistoryIndex++;
                if (maxHistoryIndex == 19) {
                    nextBT.setEnabled(false);

                } else {
                    nextBT.setEnabled(true);
                }

            } else {
                // returning to question already on list  
                currentHistoryIndex++;

                nextQuestion = questionHistory[currentHistoryIndex][0];
                int nextAnswer = questionHistory[currentHistoryIndex][1];
                setSelectedButton(nextAnswer);
            }

            if (currentHistoryIndex == 19) {
                nextBT.setEnabled(false);
            }

            currentQuestion = nextQuestion;
            questionHistory[currentHistoryIndex][0] = currentQuestion;
            questionHistory[currentHistoryIndex][1] = selectedButton();

            question.setText(questions[currentQuestion * 7]);
            rb1.setText(questions[(currentQuestion * 7) + 1]);
            rb2.setText(questions[(currentQuestion * 7) + 2]);
            rb3.setText(questions[(currentQuestion * 7) + 3]);
            rb4.setText(questions[(currentQuestion * 7) + 4]);
            rb5.setText(questions[(currentQuestion * 7) + 5]);

            previousBT.setEnabled(true);

            //setSelectedButton(questionHistory[currentHistoryIndex][1]);
            questionCountLB.setText("Question " + (currentHistoryIndex + 1) + " of 20");


            //if(bg.isSelected()){



            bg.clearSelection();
        }

    }); 

// implements previous button

    previousBT.setPreferredSize(new Dimension(120, 30));
    previousBT.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent evt) {

            nextBT.setEnabled(true);
            questionHistory[currentHistoryIndex][1] = selectedButton();

            currentHistoryIndex--;
            if (currentHistoryIndex == 0) {
                previousBT.setEnabled(false);
            }
            if (currentHistoryIndex > 0) {
                previousBT.setEnabled(true);
            }
            int nextQuestion = questionHistory[currentHistoryIndex][0];
            currentQuestion = nextQuestion;
            question.setText(questions[currentQuestion * 7]);
            rb1.setText(questions[(currentQuestion * 7) + 1]);
            rb2.setText(questions[(currentQuestion * 7) + 2]);
            rb3.setText(questions[(currentQuestion * 7) + 3]);
            rb4.setText(questions[(currentQuestion * 7) + 4]);
            rb5.setText(questions[(currentQuestion * 7) + 5]);

            setSelectedButton(questionHistory[currentHistoryIndex][1]);
            questionCountLB.setText("Question " + (currentHistoryIndex + 1) + " of 20");
        }
    });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:01:08+00:00Added an answer on June 15, 2026 at 8:01 pm

    As you described, when user hit next button, the radio buttons are cleared, if you don’t save the user’s selection, of course the user cannot check his previous answers.

    So I think you need to create a HashMap, which uses the questionIndex as the key, and answer as the value, to store the user’s selections. Each time user hits next button, just put user’s selection into hashmap with question’s ID. When user hits previous button, just get the previous question’s index and get the answer from the hashmap, and make the corresponding radio button selected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i know this is to much silly question but i dont remember how to
Ok so this maybe a simple/silly question but I don't know so here goes:
I dont know how doable this is but I'm working on a datatype for
sorry for this question, but i dont know where to search for it, or
I know this may be really silly - but how can I disable the
I know this might look silly, but I still don't know why this happens
This is an admittedly silly question, but I honestly don't know if this is
I know that it might sound silly but, while working on project at some
I know this is silly question but I don't know which step I'm missing
This might be a silly question, but I just wanted to know. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.