2013-04-09 80 views
1

我正在執行一些命令在Linux上使用JSCH和輸出該命令並在我的JAVAFX應用程序的活動日誌上打印相同。活動日誌沒有得到持續更新在JavaFX

這是一個問題:打印某行後,活動日誌區卡住了。但是,如果我從窗口切換並返回應用程序繼續在日誌區域打印行。我已經調試了好幾次,但無法解決問題。

下面是代碼

channel.connect(); 

PrintStream commander = new PrintStream(channel.getOutputStream(), true); 
commander.println(command_cd); 
commander.println(" exit;"); 
commander.close(); 


InputStream outputstream_from_the_channel = channel.getInputStream(); 
BufferedReader reader = new BufferedReader(new InputStreamReader(outputstream_from_the_channel)); 
String jarOutput; 
BufferedWriter bw = new BufferedWriter(new FileWriter(resultLogFile.getAbsolutePath(), true)); 
while ((jarOutput = reader.readLine()) != null) { 
    this.logger.info("Status Update = " + jarOutput); 
    System.out.print("Status Update on screen ="+jarOutput + "\n"); 
    bw.write(jarOutput); 
    bw.newLine(); 
    outputFromUnix.append(jarOutput).append("\n"); 

    // Display in activity log area in realtime. 
    if (DeploymentTaskController.actLogTArea != null) { 
     System.out.println("outputFromUnix.toString()---->>>>> " + outputFromUnix.toString()); 

    //actlogArea is TextArea 

     DeploymentTaskController.actLogTArea.setText(outputFromUnix.toString()); 
     DeploymentTaskController.actLogTArea.end(); 

    } 
} 
bw.close(); 

reader.close(); 

回答

0

JavaFX的有一個名爲 「JavaFX應用程序線程」 更新所有UI一個特殊的線程。此線程不應用於任何非UI操作,並且所有UI操作必須在該線程上運行

確保你在一個單獨的線程中運行你的代碼和包裝所有UI代碼爲Platform#runLater()已經執行

// Using separate thread to run data loading code 
new Thread(new Runnable() { 
    public void run() { 
     channel.connect(); 

     // [...] all your channel reading code 

     // Updating UI from JavaFX Application Thread 
     if (DeploymentTaskController.actLogTArea != null) { 
      System.out.println("outputFromUnix.toString()---->>>>> " + outputFromUnix.toString()); 

      final String outputStr = outputFromUnix.toString(); 
      Platform.runLater(new Runnable() { 
       @Override 
       public void run() { 
        DeploymentTaskController.actLogTArea.setText(outputStr); 
        DeploymentTaskController.actLogTArea.end(); 
       } 
      }); 
     } 
     bw.close(); 
     reader.close(); 
    } 
}).start(); 

還有JavaFX中一個特殊的併發API對於這樣的任務,在這裏看到:http://docs.oracle.com/javafx/2/threads/jfxpub-threads.htm

+0

非常感謝謝爾蓋! – Rohan