Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

java - JavaFx response to SwingUtilities.invokeLater

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Platform.runLater(java.lang.Runnable runnable)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...