Results 1 to 5 of 5
Thread: new thread needed?
- 03-16-2010, 08:50 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 90
- Rep Power
- 0
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(); } }); } }
- 03-16-2010, 09:36 PM #2
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
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?
- 03-16-2010, 09:53 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 90
- Rep Power
- 0
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.
- 03-16-2010, 10:13 PM #4
Member
- Join Date
- Nov 2009
- Posts
- 90
- Rep Power
- 0
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!
-
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:
YMMV. Best of luckJava 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(""); } } }
edit: too late,... you've solved it which is great. Keep up the good work!
Similar Threads
-
Difference between Thread.yield() and Thread.sleep() methods
By Nageswara Rao Mothukuri in forum New To JavaReplies: 12Last Post: 07-30-2010, 05:37 PM -
Trigger main thread method from secondary thread?
By DigitalMan in forum Threads and SynchronizationReplies: 8Last Post: 01-26-2010, 02:13 AM -
data from the main/GUI thread to another runnin thread...
By cornercuttin in forum Threads and SynchronizationReplies: 2Last Post: 04-23-2008, 10:30 PM -
If JNI thread call the java object in another thread, it will crash.
By skaterxu in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 07:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks