2017-12-11 11 views
1

Runnable을 구현하는 다른 클래스의 Platform.runLater을 사용하여 TextArea를 업데이트하려고합니다. 클래스에 내 모든 GUI가 있습니다 (내 TextArea가있는 곳입니다), GUI를 만들 때 new server 스레드를 만들고 실행하고 있습니다. 내 TextArea을 업데이트하기 위해 Server 스레드의 Platform.runLater을 사용하려고 시도했지만 Platform.runLater 캔트가 내 TextArea에 도달하지 못했습니다. Modifying JavaFX gui from different thread in different classJavaFX가 Platform.runLater를 사용하는 다른 스레드에서 TextArea를 업데이트합니다.

을하지만 자바는 늘 서버 생성자에 대한 매개 변수로 내 SimulationWindow 인스턴스를 전달하자 : 그들이 여기처럼

public class SimulationWindow { 
    public SimulationWindow instance() { 
     return this; 
    } 
    public static void DisplaySimulationWindow() throws FileNotFoundException { 
     Stage SimuStage = new Stage(); 
     SimuStage.initModality(Modality.APPLICATION_MODAL); 
     SimuStage.setTitle("Simulation Window"); 
     Server myServer = new Server(instance()); 
     Thread serverThread = new Thread(myServer); 
     serverThread.start(); 
     TextArea serverTextArea; 
     . 
     . 
     . 
} 

public class Server implements Runnable { 
    @Override 
    public void run() { 
     while(true){ 
      whileConnected(); 
     . 
     . 
    } 
    private void whileConnected() throws IOException { 

     sendMessage(message); 

     do { 
      try { 
       message = (String) input.readObject(); 
       showMessage(message); 
       . 
       . 
       . 
    } 
    private void showMessage(String x) { 
    Platform.runLater(() -> serverTextArea.appendText(x));   
    } 

나는 서버 생성자에 내 SimulationWindow의 인스턴스를 통과했습니다. 다른 솔루션은 Server 및 SimulationWindow 클래스를 하나의 클래스로 보유하지만이를 분리하여 유지하려고합니다. 팁을 주시면 감사하겠습니다!

+1

서버에서 textArea를 매개 변수로 사용하는 생성자를 추가하십시오. – kleopatra

+0

* "하지만 Java는 서버 구성 자의 매개 변수로 Java SimulationWindow 인스턴스를 전달하지 않습니다."* What' SimulationWindow' 인스턴스 : 코드에 인스턴스가 없습니다. 'displaySimulationWindow()'는 실제로'static' 일 필요가 있습니까? –

+0

안녕하세요 @ James_D 질문에 인스턴스 메서드를 추가했습니다. 그 점을 지적 해 주셔서 감사합니다. 정적 및 이제 서버 생성자에 인스턴스를 보낼 수 있지만 여전히 서버 내에서 serverTextArea 액세스 할 수 없습니다. – CCBoy

답변

0

나는 kleopatra가 제안한대로 이것을 풀었다. TextArea를 내 Server 생성자에 전달했습니다. GUI 클래스에서 내 TextArea를 업데이트하고 Server 클래스의 클라이언트에서 메시지를 가져 오려고했습니다. (나는 SimuWindow() 내 서버를 만들고 시작한다.)

public class myGUI{ 
    public void SimuWindow(){ 
     //this method creates all the GUI. 
     Server myServer = new Server(serverTextArea); 
     Thread serverThread = new Thread(myServer); 
     serverThread.start(); 

     sendingTest = new TextField(); 
     sendingTest.setPromptText("test communication here"); 
     sendingTest.setOnAction(event -> { 
     String message = new String ("\nServer says: "); 
     message = message + sendingTest.getText(); 
     serverTextArea.appendText(message); 
     myServer.sendMessage(message); 
     sendingTest.clear(); 
    }); 
    } 
} 

public class Server implements Runnable{ 
    //This is my server class that connects and listens to clients 
    TextArea mytextArea; 
    public Server(TextArea x) { 
     this.mytextArea = x; 
    } 
    private void whileConnected() throws IOException {   
    do { 
     try { 
      message = (String) input.readObject(); 
      showMessage(message);    
     }catch(ClassNotFoundException classNotFoundException) { 
      System.out.println("\n i dont know the message"); 
     } 
    }while(!message.equals("Disconnected"));  

    private void showMessage(String mess) { 
      Platform.runLater(() -> mytextArea.appendText(mess));   
    } 
} 

나는 나의 클라이언트 클래스에서 똑같이했다. 당신의 도움을 주셔서 감사합니다.