2014-06-12 17 views
0

我有一個SwingWorker線程,我用它來更新UI JLabel,並且除了publish()/ process()方法之外,程序也可以工作JLabel已成功發佈並帶有適當的文本/背景/邊框等)。但是,我想在doInBackground()完成它的工作時使用process()方法將JLabel的文本設置爲「Connecting ...」,但我的程序中的process()方法從未被調用過使用publish()方法)。建議?SwingWorker:process()方法沒有被調用

這裏的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

process的簽名是process(List<V>)而不是process(V)。將您的工藝方法更改爲process(List<String> list);您可能會在列表中獲得多個項目,具體取決於在進程有機會運行之前調用發佈的頻率。

+0

我已經做了編輯,現在確實調用了方法 - 輸出print語句 - 但JLabel沒有成功發佈文本。我試圖改變與done()方法相同的標籤是否重要? – aherbets

+0

重要的是您是否處於事件派發線程中。確保你使用SwingUtilities.invokeNow()來設置它。我不知道這是你的問題,但是當你的UI更新代碼似乎沒有更新UI時,首先要檢查它。 – arcy

+0

我認爲問題是調用doInBackground()方法太快以至於無法注意到process()方法的更改。現在我只模擬一個將會建立起來的連接,所以如果我使doInBackground()方法睡眠以模擬建立真正連接的時間,它就可以工作。謝謝你的幫助。 – aherbets