Question Swing / SwingWorker Beginer

I am trying to implement a GUI in java, but I'm starting in swing. I want to make something clear. I read that in order to maintain the responsiveness of the GUI, I must use the SwingWorker class to complete the task in a separate thread. Okay bye. No. I have a model with about 15 methods that are remote methods. Each method returns a different type of object as a result than the others. In my opinion, the user presses the button and the corresponding method in the model is called. Without using swingworker, the graphical interface froze. My question is: should I create 15 subclasses of Swingworker threads and create a NEW instance of each as necessary according to the user's actions? Do I understand correctly? Is there a standard way for this, or what I'm saying is the right approach?

Thank!

+3
source share
2 answers

Take a look at this: Simple background tasks .

You seem to have two problems. Firstly, regarding the amount of code needed when using SwingWorker: you need to create a subclass SwingWorkerfor each action, but this does not mean that they must be at the top level, they are called classes or the files themselves. They can be anonymous classes, as shown in the article, so the code is inside your GUI event handling code.

-, SwingWorker: SwingWorker, (, ), - .

+2

, SwingWorkers . , SwingWorkers . , EventDispatch . EventDispatch , .

Swing.

, , , , , -

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

    public void run() 
    { 
        // this codes runs on the event dispatch thread 
        // update the ui here.
    } 
}); 
0

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


All Articles