SwingWorker:没有调用process()方法

时间:2014-06-12 16:03:40

标签: java multithreading process publish swingworker

我有一个SwingWorker线程,我用它来更新UI JLabel,除了publish()/ process()方法之外,该程序也可以工作(因为JLabel成功发布了相应的文本/背景/边境等)。但是,我想使用process()方法将JLabel的文本设置为“Connecting ...”而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除了发布/处理方法之外还有我喜欢的功能。提前谢谢!

1 个答案:

答案 0 :(得分:1)

process的签名是process(List<V>),而不是process(V)。将您的流程方法更改为process(List<String> list);您可能会在列表中获得多个项目,具体取决于在流程有机会运行之前调用发布的频率。