Results 1 to 5 of 5
  1. #1
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Question new thread needed?

    Once again I'd like to prevail upon the Java-Forums community for help.

    I have code that will present the user (a student) a question, request an answer from him, then compare his answer with the correct answer.

    The problem is that for a "for" loop with a given number of iterations, only the last question is being displayed in the GUI. As you can see below, I have event tried putting the thread to sleep, thinking that it was blazing through the display too fast, but it doesn't work. To top all this off, the full code works just fine when I run everything through the console and not a GUI.

    Any suggestions?

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class AppGUI implements ActionListener {
    
            JLabel probLbl = new JLabel("");
            int numberInt = 10;
            JButton jBtn1;
    
            AppGUI() {
                    JFrame jFrm = new JFrame("AppGUI");
    
                    jFrm.setSize(500,500);
    
                    jFrm.setLayout(new FlowLayout());
    
                    jFrm.setDefaultCloseOperation(jFrm.EXIT_ON_CLOSE);
    
                    JPanel probPnl = new JPanel();
                    probPnl.setPreferredSize(new Dimension(500,250));
                    probPnl.add(probLbl);
    
                    jBtn1 = new JButton("START");
                    jBtn1.setActionCommand("START");
                    jBtn1.addActionListener(this);
                    jFrm.add(jBtn1);
    
                    jFrm.add(probPnl);
    
                    jFrm.setVisible(true);
            }       
    
            public void actionPerformed(ActionEvent ae) {
                    if (ae.getActionCommand().equals("START")) {
                            for (int i = 1; i <= numberInt; i++) {
                                    probLbl.setText("Problem #: " + i); 
                                    try {   
                                            Thread.sleep(1000);
                                    }       
                                    catch (InterruptedException e) {
                                            //Do nothing.
                                    }       
                            }       
                    }
            }
    
            public static void main( String[] args ) {
                    SwingUtilities.invokeLater(new Runnable() {
                            public void run() {new AppGUI(); }
                    });
            }
    }

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    Another Thread will not help you here. You need a way for the student to signal that they are done with the question and to continue onto the next one instead of using the for loop.


    Even if you sleep the thread for a second, there is no telling the user answered the question yet. Why not try adding another button or changing the start button to next when it is first clicked?

  3. #3
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default

    I see. So use an "if" statement, as in "if (currentProblemNumber <= totalProblemNumber)".

    I like the idea of changing the START button into the NEXT button.

    I'll give it a go and let you know how it works out.

    Thanks StormyWaters.

  4. #4
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Wink

    Your suggestion works great. I'm slowing figuring out how Java (and Swing) work. It's a lot different than Fortran77 ... and better.

    Thanks again StromyWaters!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I recommend that you give your class an array or ArrayList of questions, as well as an int index variable to keep track of where you're at in the array, like so:
    Java Code:
      private String[] questions = {
          "What is your name?",
          "What is your quest?",
          "What is your favorite color?",
          "What's the air-speed velocity of a fully laden swallow?"
      }; 
      private int questionIndex = 0;

    Then you start out placing question number one into your questionLabel JLabel (or whatever you call it), and each time the submit answer JButton has been pressed, you extract the asnwer from the JTextField (here called answerField), do something with the extracted text to check its correctness, clear the answerField JTextField, increment the questionIndex so you can move on to the next question, and then place the next question into the questionLabel if the index is not greater than or equal to the length of the question array (or size of the question ArrayList), like so:
    Java Code:
      private void submitAnswerActionPerformed() {
        if (questionIndex < questions.length) {
          String answer = answerField.getText();
          // TODO: do something to check this answer
          
          answerField.setText("");
          questionIndex++; 
          if (questionIndex < questions.length) {
            questionLabel.setText(questions[questionIndex]);
          }
          else {
            questionLabel.setText("");
          }
        }
      }
    YMMV. Best of luck

    edit: too late,... you've solved it which is great. Keep up the good work!

Similar Threads

  1. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 06:37 PM
  2. Trigger main thread method from secondary thread?
    By DigitalMan in forum Threads and Synchronization
    Replies: 8
    Last Post: 01-26-2010, 03:13 AM
  3. data from the main/GUI thread to another runnin thread...
    By cornercuttin in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-23-2008, 11:30 PM
  4. Replies: 0
    Last Post: 01-28-2008, 08:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •