2017-12-11 6 views
1

Runnableを実装する別のクラスからPlatform.runLaterを使用してTextAreaを更新しようとしています。 私はすべてのGUIをクラスに持っています(私のTextAreaがあるところです)、私はnew serverスレッドを作成していて、GUIを作成するときにそれを実行しています。私のTextAreaを更新するためにServerスレッドからPlatform.runLaterを使用しようとしていますが、Platform.runLaterは私のTextAreaに届きません。JavaFX update TextAreaをPlatform.runLaterを使用して別のスレッドから

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のインスタンスを渡してみました:Modifying JavaFX gui from different thread in different class

しかし、Javaの文句を言わないサーバーのコンストラクタのパラメータとしての私のSimulationWindowインスタンスを渡すことができます。 他のソリューションでは、ServerとSimulationWindowのクラスを保持していますが、それらを分離しておく必要があります。 ヒントありがとうございます!

+1

、*パラメータ – kleopatra

+0

ようにtextAreaを取るコンストラクタを追加し、「しかし、Javaは文句を言わないサーバーのコンストラクタのパラメータとしての私のSimulationWindowインスタンスを渡しましょう。」*何 'SimulationWindow'インスタンス:あなたのコード内の1つのがありません。 'displaySimulationWindow()'は本当に 'static'である必要がありますか? –

+0

こんにちは@James_D私は質問にインスタンスメソッドを追加しました。それを指摘してくれてありがとう。私は静的を削除し、今私はサーバーのコンストラクタにインスタンスを送信することができますが、私はサーバー内からserverTextAreaにアクセスできません。 – CCBoy

答えて

0

私はこれをKleopatraとして提案しました。 TextAreaをServerコンストラクタに渡しました。 GUIクラスのTextAreaを私のGUIで更新したいときや、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));   
    } 
} 

私はクライアントクラスで同じことをしました。ご協力いただきありがとうございます。サーバーで

関連する問題