How can I externally update a JavaFX scene?

后端 未结 3 926
悲&欢浪女
悲&欢浪女 2021-01-15 13:24

I am trying to learn JavaFX and convert a swing application to JavaFX. What I want to do is use JavaFX to display the progress of a program.

What I was previously do

3条回答
  •  清歌不尽
    2021-01-15 13:44

    Try calling from within the UI thread as so:

    public void setText(final String newText) {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                btn.setText(newText);
            }
        });
    }
    

    Anytime you want to make changes to an element of the UI, it must be done from within the UI thread. Platform.runLater(new Runnable()); will do just that. This prevents blocking, and other strange obscure UI-related bugs and exceptions from occurring.

    Hint: what you have read/seen with the Platform.runLater being called in the startup of the app, and on timers is usually a way to load most of the UI immediately, then fill in the other parts after a second or two (the timer) so that not to block at startup. But Platform.runLater is not only for startup, it's for any time you need to change/use/interact-with a UI element.

提交回复
热议问题