943,534 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 561
  • Java RSS
Jun 17th, 2009
0

Threading problem

Expand Post »
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.


Java Syntax (Toggle Plain Text)
  1. private void mean4LblMouseClicked(java.awt.event.MouseEvent evt) {
  2. // TODO add your handling code here:
  3.  
  4. String lblText = mean4Lbl.getText().toString();
  5.  
  6. if (lblText.equals(correctMean)) {
  7. this.mean4Panel.setBackground(Color.GREEN);
  8. }
  9. Thread.sleep(1000); //give a chance to gamer to see this result
  10. coordinator(); //move to the next question
  11. this.mean4Panel.setBackground(new Color(236, 233, 216)); //grayed the jPanel object again
  12.  
  13. }
Last edited by ~s.o.s~; Jun 17th, 2009 at 2:38 pm. Reason: Added code tags, learn to use them.
Similar Threads
Reputation Points: 8
Solved Threads: 3
Junior Poster
MxDev is offline Offline
137 posts
since Sep 2007
Jun 17th, 2009
0

Re: Threading problem

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!)
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jun 17th, 2009
1

Re: Threading problem

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.
Featured Poster
Reputation Points: 1907
Solved Threads: 947
Posting Expert
JamesCherrill is offline Offline
5,754 posts
since Apr 2008
Jun 19th, 2009
0

Re: Threading problem

You could also use an anonymous inner class and create a new Runnable and put the code you want to run in a different thread in there.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Jun 27th, 2009
0

Re: Threading problem

anyway thanks guys i'll try all of these and tell you.
thanks again
Reputation Points: 8
Solved Threads: 3
Junior Poster
MxDev is offline Offline
137 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Oracle 9i and jdbc1.4 discrepancy
Next Thread in Java Forum Timeline: alternate way for http://127.0.0.1:8080





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC