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.

private void mean4LblMouseClicked(java.awt.event.MouseEvent evt) {                                      
        // TODO add your handling code here:

        String lblText = mean4Lbl.getText().toString();

        if (lblText.equals(correctMean)) {
        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

7 Years
Discussion Span
Last Post by MxDev

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!)


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/tutorial/uiswing/misc/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.

Votes + Comments
Good suggestion.

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.


anyway thanks guys i'll try all of these and tell you.
thanks again

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.