JavaFx response to SwingUtilities.invokeLater

浪尽此生 提交于 2019-11-26 21:36:23

问题


So I am aware that JavaFx's method of updating the GUI while using a thread is called Task but does the code work in similar way or are there any differences. let me give you a swing example:

Another class outside the GUI that runs as a thread

public void run(){
    while (socket.isConnected()) {
        String x = input.next();
        System.out.println(x);
        mg.updateChat(x)
    }
}

Inside the actual GUI

public void updateChat(final String input){
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            txtChat.setText(input); 
        }
    }); 
}

Does Task work the exact same way? Or are there differences and if there are how would you modify this code to work in a JavaFx project?


回答1:


Are you looking for SwingUtil.invokeLater counterparts in JavaFX. If yes, it is:

Platform.runLater(java.lang.Runnable runnable)


来源:https://stackoverflow.com/questions/12984310/javafx-response-to-swingutilities-invokelater

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!