Java fx running on UI alternatives

2019-06-14 05:26发布

问题:

ayt.. i have a little concern here, my question is is Platform.runlater() the only way of getting back to the UI thread or are there any alternatives..

-for example-

new Thread(new Runnable() {

        @Override
        public void run() {
            // i do something slick here

            Platform.runLater(new Runnable() { // is this the only way??

                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                TextField txt =new TextField(" internal server error -Re-try");
                                root.getChildren().add(txt);
                            }
                        });

        }
    }).start();;

i'd like to know if there are other means..thanks in advance...

回答1:

As far as I know there are no other means. But you can through binding do some limited stuff to effectively change the UI from another thread without calling PlatForm.runLater() Example:

Task task = new Task<Void>() {
    @Override public Void run() {
         for (int i=0; i<100; i++) {
            updateProgress(i, 100);
        }
        return null;
    }
};

And then in your GUI have a progressBar say:

ProgressBar bar = new ProgressBar();
bar.progressProperty().bind(task.progressProperty());
new Thread(task).start();

bar.progressProperty().bind(task.progressProperty());