views:

87

answers:

3

Hi, I'm doing some routine in Java (1.5)+Swing, that damands some time. How the best way to implement this routing outside the swing thread, to avoid UI freezing?

Thanks in advance

+5  A: 

At first blush, look at the SwingWorker class. When you want to make the code more robust and testable, you probably want to move away from that, but it is a good enough first start.

You can get a version for Java 1.5 here. With 1.6 it is part of the standard API.

Yishai
I have never used the JDK implementation, but it seems to be bugged, see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6880336
Sylvain M
@Sylvain that bug is marked as applying to 1.7, not 1.6.
Ash
@Ash, this applies from 1.6u18, not just 1.7
Yishai
@Yishai, I see I didn't read down far enough.
Ash
+2  A: 

Using SwingWorker is of course good idea and I recommend that. Also writing custom javax.swing.Timers and java.lang.Threads .

But don't forget to use profiler - it can help you to find many problems. Like Swing is often having trouble with "dead" Listeners holding some references which can not be garbage collected (resulting in very slow responses or freezing of UI or even memory leaks). Profiler will help you to investigate memory needs of specific situations when using your application and therefore you might be able to do fine tuning of your app.

Xorty
A: 

Resolved as comment:

"This could help: stackoverflow.com/questions/2564388/javas-swing-threading – Andreas_D Jul 5 at 22:01"

Victor