views:

110

answers:

2

Hi, I am trying to implement a GUI in java but I am beginner in swing. I want to make something clear. I read that in order to keep the GUI responsive I should use the SwingWorker class to do the task in a separate thread. Ok so far. No I have a model with around 15 methods that are remote methods. Each method returns different object type as a result than the others. In my view the user presses a button and the appropriate method in the model is called. Without using the swingworker the GUI froze. My question is, am I supposed to create 15 subclasses of Swingworker threads and create a NEW instance of each as needed according to user's actions? Is my understanding correct? Is there a standard way for this or what I say is a correct approach?

Thanks!

+2  A: 

Have a look at this: Simple Background Tasks.

It seems you have two concerns. Firstly, regarding the amount of code required when using SwingWorker: you do need to create a subclass of SwingWorker for each action, but that doesn't mean they need to be top-level, named classes, or in their own files. They can be anonymous classes, as shown in the article, so that the code is within your GUI's event-handling code.

Secondly, regarding instantiation of SwingWorker objects: you can't reuse a SwingWorker, but since the jobs are being executed as a result of user activity (e.g. clicking a button), you shouldn't encounter any performance problems with instantiating new objects each time.

Richard Fearn
@Richard: Thank you. Will there be any difference in performance between using anonymous classes or subclasses?
I doubt it. Instances of anonymous inner classes have a reference to an instance of the enclosing class (i.e. take up more memory). You could use static nested classes instead, but then you would need to pass in some reference to your GUI so that once the job has completed, you can update the GUI. (See this if you want to know about inner vs. static nested classes: http://blogs.sun.com/darcy/entry/nested_inner_member_and_top)
Richard Fearn
A: 

By all means, SwingWorkers get the job done. In my experience, I haven't liked using the SwingWorkers for just one little job. I prefer to spawn off a thread, and have that thread ask the EventDispatch thread to update the GUI. Only the EventDispatch thread should update the UI, though there are a few exceptions.

I would suggest reading about threads in threads in Swing.

Though threading can get heavy, and maybe this solution would not work for you in all cases, if a seperate thread needs to spark a change in GUI, use something like,

java.awt.EventQueue.invokeLater(new Runnable() 
{ 

    public void run() 
    { 
        // this codes runs on the event dispatch thread 
        // update the ui here.
    } 
}); 
Andy Pryor
@Andy:Isn't there any overhead by spawing continually new threads since, in my understanding, we cannot pool them, but have a NEW thread (the way you describe) per each interaction with the gui?
Threading should be done with caution yes. There is always overhead. I think certain situations, a background thread is justified.
Andy Pryor