2016-09-17 2 views
-1

Linux 가상 머신 (JSch)에 연결하는 응용 프로그램을 만들고 Linux에 OS 이름과 커널 버전과 같은 몇 가지 질문을합니다. 나는 그것에 성공했고 애플리케이션은 작동하지만 Eclipse 콘솔에서만 작동한다.JavaFX 레이블이 텍스트를 올바르게 업데이트하지 않음

Label 또는 TextArea에서 인쇄하려고하면 이상한 일이 발생합니다. 예를 들어, 레이블에 인쇄하려고하면 마지막 명령 만 인쇄합니다.

다음
import javafx.fxml.FXML; 
import javafx.scene.canvas.Canvas; 
import javafx.scene.control.Label; 
import javafx.scene.control.TextArea; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.Pane; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.Properties; 

import com.jcraft.jsch.Channel; 
import com.jcraft.jsch.ChannelExec; 
import com.jcraft.jsch.JSch; 
import com.jcraft.jsch.Session; 

public class MainWindowController { 

    @FXML private TextField ip_text_field, username_text_field, password_text_field; 
    @FXML private Label output; 

    String ip, username, pass; 


    private Main main; 

    public void setMain(Main main){ 
     this.main = main; 
    } 

    public String getIP(){ip = ip_text_field.getText(); return ip;} 
    public String getUsername(){username = username_text_field.getText(); return username;} 
    public String getPassword(){pass = password_text_field.getText(); return pass;} 

    public void connectButtonFunction(){     

     try{ 
      String command = "lsb_release -a | grep -i Description && uname -mrs"; 

      String host = getIP(); 
      String user = getUsername(); 
      String password = getPassword(); 

      JSch jsch = new JSch(); 
      Session session = jsch.getSession(user, host, 22); 
      Properties config = new Properties(); 
      config.put("StrictHostKeyChecking", "no"); 
      session.setConfig(config);; 
      session.setPassword(password); 
      session.connect(); 

      Channel channel = session.openChannel("exec"); 
      ((ChannelExec)channel).setCommand(command); 
      channel.setInputStream(null); 
      ((ChannelExec)channel).setErrStream(System.err); 

      InputStream input = channel.getInputStream(); 
      channel.connect(); 

      //System.out.println("Channel Connected to machine " + host + " server with command: " + command); 

      try{ 
       InputStreamReader inputReader = new InputStreamReader(input); 
       BufferedReader bufferedReader = new BufferedReader(inputReader); 
       String line = null; 

       while((line = bufferedReader.readLine()) != null){ 
        //System.out.println(line); 
        output.setText(line);     

       } 

       bufferedReader.close(); 
       inputReader.close(); 
      }catch(IOException ex){ 
       ex.printStackTrace(); 
      } 

      channel.disconnect(); 
      session.disconnect(); 


     }catch(Exception ex){ 
      ex.printStackTrace(); 
     }   
    } 
} 

가 어떻게입니다 : 다음 텍스트 영역에 그것을 시도하는 경우가 여기에

코드는 ... 모두를 출력하지만, 한 줄, 그리고 내가 어떻게 브레이크 라인 모른다 라벨처럼 보입니다.

enter image description here

답변

1

당신이 setText 레이블 텍스트 매번 다시 작성하기 때문에 Label가 사용할 때 마지막 명령을 보여줍니다 이유.

그래서 이런 걸 그것을 해결할 : (당신은 또한 Label에 직면하게 될 것이다)을 TextArea 문제, 당신은 output.setWrapText(true)

주를 호출하여 텍스트 줄 바꿈을 활성화 할 수 있습니다에 관해서는

String buffer = ""; 
while((line = bufferedReader.readLine()) != null){ 
    buffer += line + "\n"; // assuming `line` doesn't end with a newline already   
} 
output.setText(buffer); 

을 : 당신이 할 수있는 여분의 버퍼가 필요 없으므로 setText 대신 appendTextTextArea에 전화하십시오.

관련 문제