Java swing long thread execution

I wrote a framework that performs calculations that take a lot of time (about 15 minutes). Now I want to write an interface in Swing that will collect data from a database and perform these calculations.

I just thought how to do it? If I do heavy computing in the event flow, the whole interface hangs until the code completes.

I began to think that I would create an Object that could handle the entire calculation task, but what should I do with the status and status information at runtime. I also have several actions that can be performed, for example. different calculations requiring different data. Do I need to write an object for each action? If I separate presentation and calculation, I have to exchange data - what is the best reason for this?

Thanks and best regards

Marco

+4
source share
2 answers

Read the section from the Swing guide to Concurrency for an example using SwingWorker.

+2
source

Take a look at SwingWorker (for Java 6, but there is a version of the library that you can get for Java 5). With SwingWorker, you start your intensive process in the doInBackground() method, and the done() method is called in the event queue when it finishes, which allows you to update the GUI. It also provides a means to notify the GUI of the progress of a task, if possible.

+3
source

Source: https://habr.com/ru/post/1301845/


All Articles