views:

76

answers:

2

Hi folks,

I have written a framework that does calculations that take a long time to execute (about 15 minutes). I now want to write an interface in Swing which will gather the data from the database and execute this calculations.

I just wondered now how to do that best practice? If I do time intensive computations in the event thread the whole interface freezes until the code is finished.

I started to think that I will create an Object which can handle all the calculation task, but what do I do with the state informations about progress and status during the execution. I also have several actions that can be executed, e.g. different calculations that need different data. Do have to write an object for each action? If I separate the view and the calculation I have to exchange the data - what is the best practice for that?

Thanks and best regards

Marco

+2  A: 

Read the section from the Swing tutorial on Concurrency for an example on using a SwingWorker.

camickr
+3  A: 

Take a look at SwingWorker (for Java 6, but there's a library version you can get for Java 5). With SwingWorker, you run your time-intensive method within the doInBackground() method and the done() method is called on the Event Queue when it finishes, allowing you to update the GUI. It also provides a means to notify the GUI of progress during the task, if possible.

ColinD
Care to explain the issue you have with this, downvoter?
ColinD
Thanks, SwingWorker sounds really interessesting
Marco Nätlitz
+1 to counter the random killjoy.
reccles
The Java 5 backport is 100% compatible; https://swingworker.dev.java.net/
trashgod