【问题标题】:SwingWorker: process() method is not being calledSwingWorker:没有调用 process() 方法
【发布时间】:2014-06-12 16:03:40
【问题描述】:

我有一个 SwingWorker 线程,我用它来更新 UI JLabel,除了 publish()/process() 方法之外,程序还可以工作(因为 JLabel 已成功发布并带有适当的文本/背景/边界等)。但是,我想使用 process() 方法将 JLabel 的文本设置为“正在连接...”,而 doInBackground() 执行它的工作,但我的程序中的 process() 方法从未被调用(我显然是使用 publish() 方法)。有什么建议吗?

这是 SwingWorker:

public class PcclientBackgroundWork extends SwingWorker < String, String> {

    public JLabel connectionStatus2;
    String msg;

    /* Constructor */
    public PcclientBackgroundWork(JLabel label){
        connectionStatus2 = label;
    }

    /*Background work to determine Application connection status
      and pass corresponding message (String msg) to done() */
    @Override
    public String doInBackground() throws Exception {

        String serverName = "localhost";                         //UDP is sent within same machine
        int port = 6789;

        try {
            Socket client = new Socket(serverName, port);
            BufferedReader in = new BufferedReader
                    (new InputStreamReader(client.getInputStream()));
            while(!in.ready()){
                publish("Connecting");                           //Want this method called until the bufferedReader is ready.
            }                                                    //Loops until ready
            msg = in.readLine();                                 //Incoming text is only one line
            if(msg.equals("Connection Unsuccessful"))
            {
                msg = "Application Connection Failed";
            } else {
                msg = "App Connected " + msg;
            }
            System.out.println("msg = " + msg);
            in.close();                                          //Close reader
            client.close();                                      //Close client socket   

        } catch (IOException e) {
            e.printStackTrace();
            msg = "Application Connection Failed";               //JLabel text set the same as 
        }                                                        //if connection is unsuccessful (lines 66-68)

        return msg;
    }


    public void process(String msg){
        System.out.println("process method called...");
        connectionStatus2.setText(msg);
    }
    /*Method to set JLabel information when doInBackground() is complete */

    @Override
    public void done() {
        try {
            connectionStatus2.setText(get());                    //get() is the return value of doInBackground (String msg)
            connectionStatus2.setBorder(BorderFactory.createLineBorder(Color.black));
            connectionStatus2.setVisible(true);
            connectionStatus2.setOpaque(true);
            if(get().equals("Application Connection Failed")){
                connectionStatus2.setBackground(Color.PINK);
            } else {
                connectionStatus2.setBackground(Color.GREEN);
            }
        } catch (InterruptedException ex) {
        } catch (ExecutionException ex) {
            Logger.getLogger(PcclientUI.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}

我没有发布 UI 线程,因为除了发布/处理方法之外,SwingWorker 的功能也符合我的喜好。提前致谢!

【问题讨论】:

    标签: java multithreading process publish swingworker


    【解决方案1】:

    process 的签名是 process(List&lt;V&gt;),而不是 process(V)。把你的处理方法改成process(List&lt;String&gt; list);您可能会在列表中获得多个项目,具体取决于在进程有机会运行之前调用发布的频率。

    【讨论】:

    • 我已经进行了编辑,现在确实调用了该方法-输出了打印语句-但是 JLabel 没有成功发布文本。我尝试更改与 done() 方法中相同的标签是否重要?
    • 重要的是您是否在事件调度线程中。确保使用 SwingUtilities.invokeNow() 进行设置。我不知道这是您的问题,但首先要检查您的 UI 更新代码何时似乎没有更新 UI。
    • 我认为问题在于 doInBackground() 方法调用太快而无法注意到 process() 方法的变化。现在我只是在模拟一个将要建立的连接,所以它运行得很快,但是如果我让 doInBackground() 方法休眠来模拟建立真实连接的时间,它就可以工作。感谢您的帮助。
    • 好的,但请确定您发出 UI 更新调用的线程。将它们放在 EDT 之外可能会导致不可预知的行为,并且插入 sleep() 可以掩盖您从错误线程调用的事实。使用 invokeWait() 永远不会受到伤害;如果它恰好是从 EDT 调用的,它会检测到并处理它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-05
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    • 2014-09-17
    • 2011-09-06
    • 2014-10-11
    相关资源
    最近更新 更多