Disable button and display message in text field at end of ArrayList iterations

415 views Asked by At

I am writing a quiz program that opens a text file containing questions and answers. The questions and answers are put into arrays and are put into text fields on button clicks. Opening the file enables the Q button.

A counter at the bottom of the page displays the total number of questions in the file as well as counts through each question asked.

When you click the "Q" button the question is placed in the Q text field, the Q button is disabled, the answer button ("A") is enabled, and the "currentQuestion" counter goes up by one. When you click the A button the answer is displayed in the answer field, the A button is disabled and the Q button is enabled. Pressing the Q button again clears the answer text from the previous question, enables the A button, and disables itself. It goes back and forth like this till it reaches the last question and answer in the arraylist.

After the last question and answer are displayed, I need to be able to press the Q button again and display the text "That's all Folks!" in the question text field and at the same time disable both the Q and A buttons.

I have managed to use a method getEndStatus() (this boolean method is a requirement of the assignment) to check if the last question has been reached. I can use this to place the text in the question text field. I cannot seem to figure out, however, how to disable both buttons. I have tried an if statement in the form's source code on the Q button click (also tried with the A button click) disabling the button if the getEndStatus() method returns true but it disables the button before the final click that produces the final message.

I also need to have each subsequent call to the JFileChooser start in the same folder last used to find a file. I have no clue how to go about doing this.

Any help is much appreciated! Thanks in advance!

HERE IS THE SOURCE CODE FROM THE CLASS CONSTRUCTOR::

package business;

import java.io.*;
import java.util.ArrayList;

public class Quiz {
private ArrayList<String> questions, answers;
private int qCount, qNumber;
private String errmsg, actmsg;

public Quiz(String fn){
    //fn = file name with full path
    this.qCount = 0;
    this.qNumber = 0;
    this.errmsg = "";
    this.actmsg = "";
    try{
        BufferedReader in = new BufferedReader(
                new FileReader(fn));
        questions = new ArrayList<String>();
        answers = new ArrayList<String>();

        String s = in.readLine();
        while (s != null){
            qCount++;
            questions.add(s); //put question in array list
            answers.add(in.readLine()); //read and post answer
            s = in.readLine(); //next question
        }//end while loop
        in.close();
    }catch (Exception e){
        errmsg = "Error opening file: " + e.getMessage();
    }//end try catch
    if (qCount > 0){
        qNumber =1;          
        actmsg = "Quiz file read. Next number = " + qNumber;
    }//end if statement
}//end of constructor

public int getQuizCount(){
    return this.qCount;
}//end getQuizCount()

public String getActionResult(){
    if (qNumber < qCount){
    actmsg = "Quiz file read. Next number = " + qNumber;
    }else if(getEndStatus()){
        actmsg = "That's all Folks!";
    }else{
    actmsg = "Quiz file read. Next number = " + qCount;
    }
    return this.actmsg;
}//end getActionResult()

public String getErrorMsg(){
    return this.errmsg;
}//end getErrorMsg()

public String getQuestion(){
    if (qNumber == 0){
        return "No Questions on File.";
    }else if(getEndStatus()){
        return "That's All Folks!";
    }else{    
    qNumber++;
    return this.questions.get(qNumber-2);
    }
}//end getQuestion()

public String getAnswer(){
    if(qNumber == 0){
        return "No Answers on File.";
    }
    return this.answers.get(qNumber-2); 
}

public int getCurrCount(){
    return qNumber-1;
}

public boolean getEndStatus(){
    if(qNumber <= qCount){
        return false;
    }
    return true;
}
}//end class()

HERE IS RELEVANT THE SOURCE CODE FROM THE FORM VIEW:

private void jmnuOpenActionPerformed(java.awt.event.ActionEvent evt) {                                         
statusMessageLabel.setText("");

JFileChooser f = new JFileChooser(".");
f.setDialogTitle("Select Quiz File");
FileNameExtensionFilter filter = 
        new FileNameExtensionFilter("Text Files (*.txt)","txt");
f.setFileFilter(filter);
JDialog dg = new JDialog();
int rval = f.showOpenDialog(dg);

if (rval == JFileChooser.CANCEL_OPTION){
    jtxtFile.setText("");
    statusMessageLabel.setText("Open Canceled.");
}else {
    jtxtFile.setText(f.getSelectedFile().getAbsolutePath());
    q = new Quiz(f.getSelectedFile().getAbsolutePath());
    if (q.getErrorMsg().isEmpty()){
        jtxtTotQ.setText(String.valueOf(q.getQuizCount()));
        jbtnQ.setEnabled(true);
        statusMessageLabel.setText(q.getActionResult());
    }else{
        statusMessageLabel.setText(q.getErrorMsg());
        jtxtTotQ.setText("");
        jtxtCurrQ.setText("");
    }//end if else    
}//end if else 
jtxtQ.setText("");
jtxtA.setText("");
}

private void jbtnQActionPerformed(java.awt.event.ActionEvent evt) {                                      
jtxtQ.setText(q.getQuestion());
jtxtCurrQ.setText(String.valueOf(q.getCurrCount()));
statusMessageLabel.setText(q.getActionResult());
jtxtA.setText("");
jbtnQ.setEnabled(false);
if (q.getEndStatus()){
    jbtnA.setEnabled(false);
}else{
    jbtnA.setEnabled(true);
}
}                                     

private void jbtnAActionPerformed(java.awt.event.ActionEvent evt) {                                      
jtxtA.setText(q.getAnswer());
jbtnA.setEnabled(false);
jbtnQ.setEnabled(true);
}                                                     
0

There are 0 answers