| | |
Threading problem
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2007
Posts: 85
Reputation:
Solved Threads: 2
hi,
I've a bug in the following code, i don't know where it is?? is it in the design itself, or the code needs threading especially in the "Swing". The following code is a part of simple word game that display a word with it's correct meaning among wrong ones, which printed in jLabel object, this object placed on jPanel object which should be greened when the gamer response is correct, otherwise it should become red, after that it moves to the next question (word) and graying the whole jPanel objects again because it's a new question.
Here's the bug if the last line of event handler which graying the panel deactivated, and try to sleep the current thread the greening process happened after not before sleeping.
So, how to coloring the jPanel object and wait for 1 second after that move to the next question and remove the coloring (graying out the jPanel object)
The following code is an event Handler of one jPanel and jLabel object for gamer mouse clicks.
I've a bug in the following code, i don't know where it is?? is it in the design itself, or the code needs threading especially in the "Swing". The following code is a part of simple word game that display a word with it's correct meaning among wrong ones, which printed in jLabel object, this object placed on jPanel object which should be greened when the gamer response is correct, otherwise it should become red, after that it moves to the next question (word) and graying the whole jPanel objects again because it's a new question.
Here's the bug if the last line of event handler which graying the panel deactivated, and try to sleep the current thread the greening process happened after not before sleeping.
So, how to coloring the jPanel object and wait for 1 second after that move to the next question and remove the coloring (graying out the jPanel object)
The following code is an event Handler of one jPanel and jLabel object for gamer mouse clicks.
Java Syntax (Toggle Plain Text)
private void mean4LblMouseClicked(java.awt.event.MouseEvent evt) { // TODO add your handling code here: String lblText = mean4Lbl.getText().toString(); if (lblText.equals(correctMean)) { this.mean4Panel.setBackground(Color.GREEN); } Thread.sleep(1000); //give a chance to gamer to see this result coordinator(); //move to the next question this.mean4Panel.setBackground(new Color(236, 233, 216)); //grayed the jPanel object again }
Last edited by ~s.o.s~; Jun 17th, 2009 at 2:38 pm. Reason: Added code tags, learn to use them.
I'm really rusty at Java but this may be an architectural thing. Your topic mentions threading but is this event handlr part of the main threads message pump? If so there's your problem. In windowing system such as Windows the primary thread also handles the graphics. Graphical functions are actually queued requests for the screen update and even when they aren't, they are painted onto an off screen frame buffer and then at system paint time, blit onto the visible frame buffer.
(Hope that helps!)
(Hope that helps!)
•
•
Join Date: Apr 2008
Posts: 1,020
Reputation:
Solved Threads: 151
Your code is executing on the Swing (event dispatch) thread, so all screen updates are held up until your method returns. That's why there's no screen update when you sleep. It's only after that method completes that the screen is updated.
Solution is to use a Swing Timer http://www.j2ee.me/docs/books/tutori...isc/timer.html
In your method set the green colour, then start a swing timer for 1 second that turns the colour back to grey when the timer expires. Your method returns immediately, the green change is drawn, then 1 sec later the timer expires and the change to grey is implemented.
Solution is to use a Swing Timer http://www.j2ee.me/docs/books/tutori...isc/timer.html
In your method set the green colour, then start a swing timer for 1 second that turns the colour back to grey when the timer expires. Your method returns immediately, the green change is drawn, then 1 sec later the timer expires and the change to grey is implemented.
![]() |
Similar Threads
- Threading Problem (VB.NET)
- Inconsistent (and wrong) output using boost::thread (C++)
- problem on threading (C#)
- Parallel threading problem (C#)
- Multi threading problem. (C++)
Other Threads in the Java Forum
- Previous Thread: Oracle 9i and jdbc1.4 discrepancy
- Next Thread: alternate way for http://127.0.0.1:8080
Views: 407 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint android animated api apple applet application arguments array arrays automation binary blackberry block bluetooth chat class classes client code component database developmenthelp draw eclipse encode error event exception file fractal game gameprogramming givemetehcodez graphics gui helpwithhomework html ide image input integer iphone j2me j2seprojects java javac javaprojects jmf jni jpanel julia lego linux list loop loops mac map method methods mobile netbeans newbie notdisplaying number object online oracle print problem program programming project recursion scanner screen server set singleton size sms socket sort sql string swing system template test textfields threads time title transfer tree tutorial-sample update windows working






