2017-01-25 7 views
0

私はJavaFxアプリケーションを使っています。別のクラスからTextFlowを更新してください。JavaFx

私は2つのクラスがあります。

私は2つの異なるスレッドでこれらのクラスを開始します。サーバーはブロック可能です。

  • (私はこれを変更する必要があることがわかります))。
  • サーバクラス(Called Serveur))。

私のサーバークラスでは、ServerSocketでバイトを受け取るとき。

私のUIクラスのTextFlow(flowMessafe)を更新する必要があります: with flowMessage.getChildren()。add();自分のUIクラスでこのTextFlowを問題なく更新できます。 しかし私は私のサーバークラスはできません。

編集:私はすべてのことを試していますが、私は大きな問題を発見したと思います。私はjavafxアプリケーションの間違ったインスタンスを更新します。

public void recevoirMessage() { 
    output = new ByteArrayOutputStream(); 
    try { 
     socket = serverSocket.accept(); 
    } catch (IOException ex) { 
     System.out.println("Can't accept client connection. "); 
    } 
    try { 
     in = socket.getInputStream(); 
    } catch (IOException ex) { 
     System.out.println("Can't get socket input stream. "); 
    } 
    byte[] bytes = new byte[16*1024]; 
    try { 
     while ((count = in.read(bytes)) > 0) {} 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
    System.out.println("Message reçus"); 
    String recu = "Message : "+new String(bytes); 
    System.out.println(recu); 
    Platform.runLater(() -> { 
     Label mes = new Label(new String(bytes)); 
     med.cli.flowMessage.getChildren().add(mes); 
    }); 
    try { 
     in.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

そして、私のメインの中で、私が持っている:私は、この機能にアクセスしてくださいメッセージが表示されたら彼は

My Server Constructor 
public Serveur(Mediator med){ 
    this.med=med; 
    try { 
     lancer(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 


Loop to catch message or file. 
for(;;){ 
     try { 
      Thread.sleep(1L); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     System.out.println("Attente de communication ..."); 

     try { 
      socket = serverSocket.accept(); 
     } catch (IOException ex) { 
      System.out.println("Can't accept client connection. "); 
     } 

     try { 
      in = socket.getInputStream(); 
     } catch (IOException ex) { 
      System.out.println("Can't get socket input stream. "); 
     } 

     byte[] bytes = new byte[16*1024];   
     while ((count = in.read(bytes)) > 0) {} 

     String mode = new String(bytes); 
     String[] split = mode.split(";"); 
     if(split[0].compareTo("Message")==0){ 
      recevoirMessage(); 
     } else if(split[0].compareTo("Fichier")==0){ 
      recevoirFichier(split[2]); 
     }  
     in.close(); 
     socket.close(); 

    } 

私のサーバーコードの一部である現在のコードで

を、コードを変更します空のコンストラクタのみ

public Main(){} 

私のUIクラスで私はこれを使って私のアプリケーションを作成します:

'@Override 
public void start(Stage primaryStage) { 
    try { 
     this.pStage = primaryStage; 
     this.pStage = new Stage(); 
     idPane = new BorderPane(); 
     Parent page; 
     page = FXMLLoader.load(getClass().getResource("/application/application.fxml")); 
     this.pStage.setTitle("Messagerie"); 
     Scene scene = new Scene(page); 
     flowMessage = new TextFlow(); 
     idContenuMessage= new ChoiceBox<String>(); 
     scrollPane= new ScrollPane(); 
     //flowMessage = new TextFlow(); 
     String css = this.getClass().getResource("application.css"). 
toExternalForm(); 
       scene.getStylesheets().clear(); 
       scene.getStylesheets().add(css); 
     this.pStage.setScene(scene); 
     this.pStage.setResizable(false); 
     this.pStage.show(); 
     this.pStage.setOnCloseRequest(event -> { 
      Serveur.close(); 
     }); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
}' 

私のサーバーTextFlowを更新する方法はわかりません。

私はメディエータパターンのようなさまざまなものを見ましたが、私はこれを試してみましたが、うまくいきませんでした。

私は、このクラスで私のアプリを起動します。

package communication; 

import application.Main; 
import javafx.application.Application; 
import javafx.stage.Stage; 

public class Mediator extends Application implements Runnable { 

private Serveur serv; 

public Main cli; 

public Thread thread; 

private Stage primaryStage; 

public static void main(String args[]){ 
    launch(args); 
} 

public Mediator(){ 
    cli = new Main(); 
    thread = new Thread(this,"serv"); 
    thread.start(); 
} 

@Override 
public void run() { 
    setServ(new Serveur(this)); 
} 

@Override 
public void start(Stage stage) throws Exception { 
    primaryStage = stage; 
    cli.start(primaryStage); 
} 

public Serveur getServ() { 
    return serv; 
} 

public void setServ(Serveur serv) { 
    this.serv = serv; 
} 
} 

感謝を助けるために。

答えて

0

Platform.runLater()を使用して、GUIの変更をJavafxアプリケーションスレッドに適用します。

public void recevoirMessage() { 
output = new ByteArrayOutputStream(); 
try { 
    socket = serverSocket.accept(); 
} catch (IOException ex) { 
    System.out.println("Can't accept client connection. "); 
} 
try { 
    in = socket.getInputStream(); 
} catch (IOException ex) { 
    System.out.println("Can't get socket input stream. "); 
} 
byte[] bytes = new byte[16*1024]; 
try { 
    while ((count = in.read(bytes)) > 0) {} 
} catch (IOException e1) { 
    // TODO Auto-generated catch block 
    e1.printStackTrace(); 
} 
System.out.println("Message reçus"); 
String recu = "Message : "+new String(bytes); 
System.out.println(recu); 

//inserted here 
Platform.runLater(new Runnable() { 
     @Override public void run() { 
      //HERE I WANT TO UPDATE MY TEXTFLOW 
     } 
    }); 


try { 
    in.close(); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

}

+0

おかげで、私はこれを試して行きます。 – Wariie

+0

あなたはこのようなTextFlowにアクセスできると思います。 'Mediator.cli.flowMessage.getChildren()。add()' – Nash

+0

Mainの良いインスタンスを更新しません:/。 – Wariie

0

まずTextFlowあなたはそれにその値を設定することはできませんので、textプロパティを持っていません。テキストフローにTextオブジェクトを追加します。

Text text = new Text();  
flowMessage = new TextFlow(text); 

その後TextコンポーネントtextPropertyにバインドし、サーバ・クラスでこの値を更新し、StringPropertyを作成します。

Applicationクラス:

public class Mediator extends Application implements Runnable { 

    private Serveur serv; 

    public Main cli; 

    private StringProperty textProperty = new SimpleStringProperty("text"); 

    public Thread thread; 

    private Stage primaryStage; 

    public static void main(String args[]){ 
     launch(args); 
    } 

    public Mediator(){ 
     cli = new Main(this,textProperty); 
     thread = new Thread(this,"serv"); 
     thread.start(); 
    } 

    @Override 
    public void run() { 
     serv = new Serveur(this,textProperty); 
    } 

    @Override 
    public void start(Stage stage) throws Exception { 
     primaryStage = stage; 
     cli.start(primaryStage); 
    } 
} 

パスtextPropertyServeurMainへとクラス。 Textコンポーネントへ

cli = new Main(this, textProperty); 
serv = new Serveur(this, textProperty); 

バインドtextプロパティ:

text.textProperty().bind(textProperty); 

最後に、あなたのServeurクラスでのJavaFXアプリケーションスレッドでtextPropertyを更新:

public void recevoirMessage() { 
    output = new ByteArrayOutputStream(); 
    try { 
     socket = serverSocket.accept(); 
    } catch (IOException ex) { 
     System.out.println("Can't accept client connection. "); 
    } 
    try { 
     in = socket.getInputStream(); 
    } catch (IOException ex) { 
     System.out.println("Can't get socket input stream. "); 
    } 
    byte[] bytes = new byte[16*1024]; 
    try { 
     while ((count = in.read(bytes)) > 0) {} 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
    System.out.println("Message reçus"); 
    String recu = "Message : "+new String(bytes); 
    System.out.println(recu); 

    //inserted here 
    Platform.runLater(new Runnable() { 
      @Override public void run() { 
       //HERE I WANT TO UPDATE MY TEXTFLOW 
       textProperty.setValue(new String(bytes)); 
      } 
     }); 


    try { 
     in.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
+0

はい、私のアプリでは私のtextFlowに挿入するためにラベルを使用しています。ありがとう、私は時間があるときにそれを試しに行く。 – Wariie

+0

'Label'も使用できます。 – MBec

+0

私はそれを試しても何も起こらない。 – Wariie

関連する問題