0

I am currently making a Who Wants To Be A Millionaire game, I am quite new to GUI but I read my questions and options from a text file, I have previously made the same game on a CLI and I used the scanner class to make the system wait for an input before moving on to the next question, but I am not too sure how to do the same with a GUI, it reads all the questions without stopping. Is there a way to make it wait between questions? or any better options? I don't want to hard code the questions.

Here is the main code that does the question reading part

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
        String answer = null;
        String question = null;
        String a = null;
        String b = null;
        String c = null;
        String d = null;
        
        InputOutput getIO = new InputOutput();
        getIO.readQuestions();
        
         for(int counter = 0; counter < 15; counter++)
         { 
          // int q = 0;
            question = getIO.readQuestion(); // Read the question
            a = getIO.readA(); // Read option a
            b = getIO.readB(); // Read option b
            c = getIO.readC(); // Read option c
            d = getIO.readD(); // Read option d
            
           // System.out.println(a);
            // Stored the correct answer from the file for later usage
            answer = getIO.readAnswer();
            
            jTextField1.setText(question);
            jTextField2.setText(a);
            jTextField3.setText(b);
            jTextField4.setText(c);
            jTextField5.setText(d);
            
        }
        
    }                                     

Here is the InputOutput class

 @Override
     public void readQuestions() // Method to read the text file
    {
        try
        {
            in = new FileReader("./resources/Questions.txt"); 
            read = new BufferedReader(in);
        }
        catch(FileNotFoundException e){
            System.out.println("File not found");
        }   
    }

   @Override
   public String readQuestion() // Read the first line of the question
    {
        try
        {
           getQuestion  = read.readLine();
          // System.out.println(getQuestion);
        }
        catch(IOException e){
            System.out.println("Error reading from file");
        }
        
        return getQuestion;
    }
   
    @Override
   public String readA() // Read option 'a'
   { 
        try
        {
           getA  = read.readLine();
         //  System.out.println(getA);
        }
        catch(IOException e){
            System.out.println("Error reading from file");
        }
        
        return getA;
   }
   
    @Override
   public String readB() // Read option 'b'
   { 
        try
        {
           getB  = read.readLine();
           //System.out.println(getB);
        }
        catch(IOException e){
            System.out.println("Error reading from file");
        }
        
        return getB;
   }
   
    @Override
   public String readC() //Read option 'c'
   { 
        try
        {
           getC  = read.readLine();
           //System.out.println(getC);
        }
        catch(IOException e){
            System.out.println("Error reading from file");
        }
        
        return getC;
   }
   
    @Override
   public String readD() // Read option 'd'
   { 
        try
        {
           getD  = read.readLine();
           //System.out.println(getD);
        }
        catch(IOException e){
            System.out.println("Error reading from file");
        }
        
        return getD;
   }
   
    @Override
   public String readAnswer() // Read answer
   { 
        try
        {
           getAnswer  = read.readLine();
        }
        catch(IOException e){
            System.out.println("Error reading from file");
        }
        
        return getAnswer;
   }

This is how the text file is arranged

What does 'NFL' stand for
a) National Food League
b) National Federation League
c) National Football League
d) National Fighting League
c
what is 9+2*5
a) 55
b) 60
c) 19
d) 40
c
  • 3
    Read all your questions/answers into some kind of model. Have a "marker" which keeps track of the current question been asked/presented. Use this information to present the question to the user and offer them a selection of possible answers. When the user selects a option, validate it, ask the model for the "next" question and move on – MadProgrammer Sep 27 '21 at 21:19
  • 1
    This actually has *nothing* to do with NetBeans or its GUI builder and all to do with Swing GUI behavior. Also yours is an [XY Problem](https://xyproblem.info/) type question where you're asking how to solve the issue in the wrong way. Have your program read the *entire* text file from the beginning, and then have the program wait to change its state depending on user input -- in other words, wait for events to occur using listeners, and then respond to them. – DontKnowMuchBut Getting Better Sep 27 '21 at 21:20
  • 1
    For [example](https://stackoverflow.com/questions/31602113/listener-placement-adhering-to-the-traditional-non-mediator-mvc-pattern/31604919#31604919) and [example](https://stackoverflow.com/questions/30925564/why-is-my-jlabel-not-showing-up/30926625#30926625) – MadProgrammer Sep 27 '21 at 21:21
  • 2
    GUIs by their nature are event driven, that is, something happens and you respond to it. So the problem is with your understanding of how GUI frameworks work. Maybe start with [Creating a GUI With Swing](https://docs.oracle.com/javase/tutorial/uiswing/). I'd also discourage you from using form editors as they don't help you learn or understand how the API works and tends to "encourage" bad patterns – MadProgrammer Sep 27 '21 at 21:32
  • Thank you guys for your help I will look into it right now – Justformaddenpurposes Sep 28 '21 at 08:29

0 Answers0