Threading problem

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2007
Posts: 81
Reputation: MxDev has a little shameless behaviour in the past 
Solved Threads: 2
MxDev MxDev is offline Offline
Junior Poster in Training

Threading problem

 
0
  #1
Jun 17th, 2009
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.


  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Threading problem

 
0
  #2
Jun 17th, 2009
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!)
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,016
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 150
JamesCherrill JamesCherrill is offline Offline
Veteran Poster

Re: Threading problem

 
1
  #3
Jun 17th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,637
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is online now Online
Posting Virtuoso

Re: Threading problem

 
0
  #4
Jun 19th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 81
Reputation: MxDev has a little shameless behaviour in the past 
Solved Threads: 2
MxDev MxDev is offline Offline
Junior Poster in Training

Re: Threading problem

 
0
  #5
Jun 27th, 2009
anyway thanks guys i'll try all of these and tell you.
thanks again
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC