Search code examples
javamultithreadingprocesspublishswingworker

SwingWorker: process() method is not being called


I have a SwingWorker thread which I'm using to update a UI JLabel, and aside from the the publish()/process() methods, the program works (in that the JLabel is successfully posted with the appropriate text/background/border etc.). However, I want to use a process() method to set the text of the JLabel to "Connecting..." while the doInBackground() does its work, but the process() method in my program is never called (I am obviously using the publish() method). Suggestions?

Here's the 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);
        }

    }

}

I did not post the UI thread because the SwingWorker functions to my liking aside from the publish/process methods. Thanks in advance!


Solution

  • The signature for process is process(List<V>), not process(V). Change your process method to process(List<String> list); you may get more than one item in the list, depending on how often publish was called before process has a chance to run.