aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Timer Trouble (again) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Timer Trouble (again)" Watch "Timer Trouble (again)" New topic
Author

Timer Trouble (again)

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
I've located the source of my earlier timer problem.
The test code shown below works fine:


However, when I modify the mouseReleased method to:


The timer no longer works. (The invokeLater method is there to allow the GUI to repaint itself first, instead of after the code inside the run() method of someRunnableObject completes, which is what tends to happen)

Even better would be to put the timer.start() inside the run() method of the someRunnableObject, but that doesn't work either.

Does anyone know how to resolve this?

Thanks very much for all your help
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
assuming 'someRunnableObject' is a long computation, by running it via
SwingUtilities, you are blocking the EDT.

run this, click the button, the timer works.
then click the label, the label will change text (show how long it takes)
before the "1 second has passed" message is printed. lbl text will = "10 secs"




if you reverse your thinking here, it works.

by reverse, I mean you have to run your long computation via a separate thread,
then, when finished, if the gui is to be updated (icon change, whatever),
this is where you use SwingUtilities - to do the update in the EDT

see if you can follow this - repeat the steps for the first example,
but now, after clicking the label, the message "1 second has passed will
print out when expected, and the label will display 10 secs (after 10 secs)

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Thanks very very very .... (to infinity) much.
I never thought of doing it like that, it makes sense now.
But I'm not sure what you meant about blocking the EDT, I don't even
know what an EDT is. But I knew it was something to do with threads

I really appreciate the time and effort you've spent helping me,
it is tricky programming, and very specific. I couldn't just open a book, and find the solution, unlike with for loops or something.

Thanks again, I'll let you know if I run into any more problems.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
I ammended my code and it still won't work


I simply switched

for

I set the search to depth 6, which takes about 6 seconds, and only after it finishes does the timer print to screen.

But the timer is set to 3 seconds, so it obviously isn't working?


[ May 12, 2006: Message edited by: colin shuker ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
EDT is the Event Dispatch Thread.

all the swing stuff travels down that road, painting, events etc
so, if you start to do a 'longTask', the EDT road is blocked until the
longTask is completed.

by creating a separate thread for longTask, you allow the EDT to continue on
and do its stuff of painting, dispatching events (timer etc).

yuo will probably have to go through all of your longTask code and see if
there is any interaction with the GUI. If there is that will/should/maybe bring it back into the EDT, and cause another blockage.

you may even have to redesign your longTask.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
My search method is just standard java, it doesnt interact with the GUI.
It just finds the best move, as a String.

for example, if i set the timer to 3 seconds,
and the task completes in 1, the timer will print to screen in 3 seconds

If the task completes in 10 seconds, the timer wont print to screen until task is done.

But it works, when I call it after clicking the start button.

Its because of the invokeLater thing, that stops it working right when the mouse is released, its so annoying, and so time wasting.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> My search method is just standard java, it doesnt interact with the GUI.
> It just finds the best move, as a String.

start a new java file(totally away from your gui project),and put the search method in there.
do NOT import javax.swing, or java.awt
hard code a value to pass to it, hard code an arraylist of moves or whatever
it is searching, and see if it compiles/runs OK.

if all that is OK, you may have to copy your project into a backup file and start stripping bits away, to narrow it down to the culprit.
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
At the moment, I've got about 4 or 5 classes all in the same file.
I'll try splitting it into separate files

But I gotta sleep now, its 4am (in uk here) so I need to sleep.
Thanks for your help
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
I tried putting the search part in a separate file, with no imports.
That didn't work.
I then put the main method into the GUI class, still doesnt work

the timer returns but only after the search has completed

I'm running out of ideas now, I think it will work if I don't use the invokeLater() method, but I need that so it can draw to the screen first, and then start the search.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Timer Trouble (again)