简体   繁体   English

如何使用SwingWorker实时更新GUI?

[英]How to use SwingWorker to update the GUI in real-time?

I'm trying to present messages in a JTextArea whenever a method's tasks are done. 每当方法的任务完成时,我都试图在JTextArea显示消息。 My methods take some time. 我的方法需要一些时间。 This is about connecting to the network or using I/O, you already know these tasks take time. 这是关于连接到网络或使用I / O的,您已经知道这些任务需要时间。

To make the application more user-friendly, I want to show messages in a JTextArea . 为了使应用程序更加用户友好,我想在JTextArea显示消息。 I've been stuck on this issue since yesterday. 自昨天以来,我一直在这个问题上陷入困境。 I asked a question about this this morning, and got some advice on how to achieve my goal. 今天早上我问了一个问题,并就如何实现我的目标获得了一些建议。 It appears that SwingWorker is helpful for performing background tasks while continuing to update the GUI. 看起来SwingWorker在继续更新GUI的同时有助于执行后台任务。

So I've tried to use this class, seen many examples and Java documents, but I really don't know how to implement this in my application. 因此,我尝试使用此类,查看了许多示例和Java文档,但是我真的不知道如何在我的应用程序中实现该类。 To help you understand, I made the example below: 为了帮助您理解,我给出了以下示例:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

class MyMethods {

// Let's suppose this task takes a long time.
public static String firstTask(){
    return "\nfirst task work done.";
}

// Let's suppose this task takes a long time.
public static String secondTask(){
    return "\nsecond task work done.";
}
}


class TaskCounter extends SwingWorker<Integer, Integer> {

public TaskCounter(){}

protected Integer doInBackground(){
    MyMethods.firstTask();
    MyMethods.secondTask();
    return null;
}

protected void done(){
    MyFrame.textArea.append(MyMethods.firstTask());
    MyFrame.textArea.append(MyMethods.secondTask());
}

}

class MyFrame extends JFrame implements ActionListener {

static JPanel panel;
static JTextArea textArea;
static JButton startButton;
TaskCounter task = new TaskCounter();

public MyFrame(){
setSize(200,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

panel = new JPanel();
textArea = new JTextArea(20,20);
panel.setLayout(new GridLayout(2,2));
startButton = new JButton("START");

panel.add(textArea);
panel.add(startButton);
startButton.addActionListener(this);
add(panel);

setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {    
    task.execute();
}

}

public class SwingWorkerTest {
public static void main(String args[]) {
    MyFrame frame = new MyFrame();

}
}

What I want to do is show messages, not at once, but one by one. 我想做的是显示消息,而不是一次,而是一一显示。 When firstTask() is finished, show the message "first work done" , and then some time later , when secondTask() finishes, show the message "second work done" . firstTask()完成时,显示消息"first work done"然后过一会儿 ,当secondTask()完成时,显示消息"second work done" Please assume that the firstTask() and secondTask() methods take some time to complete. 请假设firstTask()secondTask()方法需要一些时间才能完成。

I've learend that long-running tasks should be put into the doInBackground() method, and methods related to updating the GUI should be put into the done() method. 我已经知道应该将长时间运行的任务放入doInBackground()方法中,并将与更新GUI相关的方法放入done()方法中。

How can I solve this problem? 我怎么解决这个问题?

Thanks for reading, and sorry for awkward expression. 感谢您的阅读,并为您的尴尬表达感到抱歉。

your background threads should call method 您的后台线程应该调用方法

String message="hi im processx";
SwingWorker.publish(message);

and you have to ovveride method 而且你必须ovveride方法

SwingWokrer.process().

this method is called on EDT when it is free with the message which was passed before to publish (might be one or more) in the argument. 如果EDT空闲,并且在参数中发布(可能为一个或多个)之前传递的消息,则在EDT上调用此方法。

You should display your progress there 您应该在那里显示进度

Yo can read more about those methods in SwingWorker documentation 可以在SwingWorker文档中阅读有关这些方法的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM