JavaFX任务/服务显示警报

时间:2016-02-18 00:52:24

标签: javafx

您好我正在尝试实现一个在LAN上传输文件/文本的简单套接字。当然,我使用JavaFX应用程序线程来显示主UI(客户端线程),并使用一个单独的线程来实现ServerSocket"接受"传入连接(服务器线程)。在加载UI时,当我尝试在服务器线程中显示Alert时,它不会让我,因为我不再使用JavaFX应用程序线程了。我试着:

  1. 在服务器线程中使用Platform.runlater()来调用Alert,如下所示:

    //inside the run() method of the Server class, which implements Runnable
    Platform.runLater(() -> {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setContentText("This shows that it works.");
        alert.show();
    });
    
    //and in Main class, which defines and displays GUI
    Thread server = new Thread(new Server());
    server.start();
    
  2. 我还尝试在服务器线程中实例化一个JFXPanel,它应该隐式实例化一个JavaFX Application对象(我​​猜)
  3. 将服务器线程转换为javafx.concurrent.Task并创建一个新的javafx.concurrent.Service子类,用于实例化服务器" Task"
  4. 但到目前为止,我无法让它发挥作用。对于第一种和第三种方法,GUI无限期挂起,对于第二种方法,抛出IllegalStateException表示我不在JavaFX应用程序线程上。

    有没有办法在JavaFX Application线程以外的线程中安全地显示Alert(或其他组件,如FileChooser)?谢谢!

0 个答案:

没有答案