Loading files asynchronously using SwingWorker: method communicationproblem

R

Royan

In my app I have two methods load() and parse() that handle
correspondingly reading from file operation and parsing that file into
my internal data structure. load() method makes use of SwingWorker
class and therefore once I enter SwingWorker#done() i must somehow
call parse() method. I can not do it from SwingWorker#done() directly
because this is logically wrong to call parse form load

So can you give me some advice on what is best way to call parse()?
 
M

Mark Space

Royan said:
In my app I have two methods load() and parse() that handle
correspondingly reading from file operation and parsing that file into
my internal data structure. load() method makes use of SwingWorker
class and therefore once I enter SwingWorker#done() i must somehow
call parse() method. I can not do it from SwingWorker#done() directly
because this is logically wrong to call parse form load

So can you give me some advice on what is best way to call parse()?

Well, how would one normally call parse()? If there's some driver class
that normally does this, for example, you could

public class MyParserDriver {

public void loadParse() {
load();
parse();
}
}

Then use the Swing worker to invoke the loadParse() method of
MyParserDriver.

If you have something a bit more detailed in mind, I think you'll have
to explain it. parse() must wait for load() to finish (I suppose), so
basically you have to wait for the worker thread to finish, then you can
call parse(). Since I think you are calling load() in response to a GUI
event, it makes sense to me to put both load() and parse() in a single
method and then invoke the worker thread on the whole thing.

You may also want to look at MVC, and separate the concerns more than
I've shown, but that's up to you.
 
R

Royan

Well, how would one normally call parse()? If there's some driver class
that normally does this, for example, you could

public class MyParserDriver {

public void loadParse() {
load();
parse();
}

}

Then use the Swing worker to invoke the loadParse() method of
MyParserDriver.

If you have something a bit more detailed in mind, I think you'll have
to explain it. parse() must wait for load() to finish (I suppose), so
basically you have to wait for the worker thread to finish, then you can
call parse(). Since I think you are calling load() in response to a GUI
event, it makes sense to me to put both load() and parse() in a single
method and then invoke the worker thread on the whole thing.

You may also want to look at MVC, and separate the concerns more than
I've shown, but that's up to you.

Thanks Mark! That's a good idea to somehow execute both methods from
SwingWorker. I've been thinking about MVC but implementing
PropertyChangeSupport and Controller class just to call one method was
a bit "too much" for such a simple problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top