• Post Reply Bookmark Topic Watch Topic
  • New Topic

Threading issues in Swing...  RSS feed

 
Travis Dean
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I've just finished writing my second Swing app and I have some possibly naive questions about threading.

First, I have broken my app up into a view class and a model/controller class. All code for manipulating the UI is in the view class and is called by the controller as needed.

I've read about SwingUtilities and the fact that all code that modifies Swing controls should be run on the event dispatcher thread. I've also read about SwingWorker and the fact that code that takes a long time to run should *not* run on the event dispatcher thread.

I'm doing some fairly time-consuming processing in my ActionListeners, therefore I'm creating SwingWorker threads and doing the processing there. However, in the middle of processing I need to do things like append text to JTextAreas and update JProgressBars, etc, so in my view code I have methods that look like this (where "excludedFiles" is a JTextArea):

It's a bit cumbersome to do the check to see if I'm on the event dispatch thread each time I modify the GUI but I want be able to call this method either on the edt or off of it, so I don't see any other way.

My first question is if this approach is on the right track or not, or am I making things needlessly complicated?

My second question is if I also have to put the code for building my GUI on the event dispatch thread or not? None of the examples in the Swing Tutorial do, but hardly any of them use SwingWorkers, either. Does building the GUI fall under the same rules as modifying it?

My third question is that I seem to be calling SwingUtilities.invokeLater() a lot and making a lot of SwingWorkers and I'm wondering if this is normal. I'm not used to multi-threaded programming so it could just be my inexperience making me uneasy about it.

Thanks for any advice anyone can offer...
[ August 09, 2006: Message edited by: Travis Capener ]
 
Travis Dean
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To answer my own question #2, it does seem that building the GUI should be done on the event dispatch thread, as illustrated in the Swing Tutorial (doh!).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!