Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why do initComponent() in work thread?  RSS feed

 
Qunfeng Wang
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The code above is put in to a SwingWorker instance. Is this totally wrong? The application looks ok now. But I want to know if it will be ok forever. Do I change this code or let it remain there.

Thanks.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code above is put in to a SwingWorker instance. Is this totally wrong?
It depends on what you are doing. SwingWorker is usually used for complex work in background threads where update communications are possible and useful or necessary.
The application looks ok now. But I want to know if it will be ok forever.
Hard to say.
Do I change this code or let it remain there.
Depends on what you are doing. The designers recommend starting an app in a new Runnable to avoid possible problems with events being generated by components before the app is properly initialized. The main problem was with ComponentListener in rare circumstances. SwingWorker could be a little heavy–handed for this. Maybe you're doing some other things with it.
 
Qunfeng Wang
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.
I know the heavy task should be executed in a background thread. That's also why Sun introduce SwingWorker. In the Java Tutorial, it's suggested to invoke the initComponent() in Event-Dispathc-Thread. But the code I've gotten isn't that case. What confused me is it does GUI stuff in a SwingWorker thread which is supposed not to do GUI or event handling task.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!