从另一个类JavaFx更新TextFlow

从另一个类JavaFx更新TextFlow,java,object,javafx,textflow,Java,Object,Javafx,Textflow,我在这里使用我的JavaFx应用程序 我有两门课: 我用两种不同的思路开始这门课。因为服务器是可阻塞的 我的UI类(称为Main(我知道我需要更改它)) 一个服务器类(称为Serveur)) 在我的服务器类中,当我使用ServerSocket接收字节时 我需要更新UI类中的TextFlow(称为flowMessafe): 使用flowMessage.getChildren().add();我可以在UI类中毫无问题地更新此文本流。 但是我的服务器类我不能 编辑:我尝试了所有的事情,但我认为我发

我在这里使用我的JavaFx应用程序

我有两门课:

我用两种不同的思路开始这门课。因为服务器是可阻塞的

  • 我的UI类(称为Main(我知道我需要更改它))
  • 一个服务器类(称为Serveur))
在我的服务器类中,当我使用ServerSocket接收字节时

我需要更新UI类中的TextFlow(称为flowMessafe): 使用flowMessage.getChildren().add();我可以在UI类中毫无问题地更新此文本流。 但是我的服务器类我不能

编辑:我尝试了所有的事情,但我认为我发现了一个大问题。我更新了javafx应用程序的错误实例。我用当前代码更改代码

他是我服务器代码的一部分

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 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();
    }
}
在我的主要部分,我只有空的构造函数,比如

 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();
    }
}'

感谢您的帮助。

我将使用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();
}   

}

首先
TextFlow
没有文本属性,因此无法将该值设置为它。将
Text
对象添加到文本流:

Text text = new Text();    
flowMessage = new TextFlow(text);
然后创建
StringProperty
,将其绑定到
Text
组件
textProperty
,并在服务器类中更新此值

应用程序类别:

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);
    }   
}
textProperty
传递给
Main
Serveur

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();
    }   
}

谢谢,我去试试这个。我想你可以像这样访问文本流
Mediator.cli.flowMessage.getChildren().add()
如果无法重现您的问题,则无法解决它。请为我提供一个可以在我的机器上运行的应用程序。你喜欢我给你发送我的应用程序的rar吗?你尝试了什么代码,它给了你什么错误?可能您使用的命令错误。是的,但在我的应用程序中,我使用标签在我的文本流中插入,这也有效吗?谢谢,我有时间就去试试。你也可以使用
标签
。我试过了,但什么也没发生。我调用了我的javafx类的错误实例。@MBec Offtopic:我刚才查看了你的编辑。。。提示:开始编辑时,请尝试解决该问题/答案的所有问题。意思:不要只是修改代码格式;还要注意其他问题,例如拼写。
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();
    }   
}