Java fx running on UI alternatives

℡╲_俬逩灬. 提交于 2019-12-08 10:52:11

问题


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());


来源:https://stackoverflow.com/questions/27113633/java-fx-running-on-ui-alternatives

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