小编Kav*_*ora的帖子

Platform.runLater() 如何发挥作用?

我有一个简单的应用程序,它在后台更新数据,当它更新时,它会禁用所有其他按钮并启用 TextArea 来显示进度。

脚步:

  1. 禁用 mainUI 中的所有其他按钮(按钮名称:plotButton)

  2. 启用显示更新已开始的 TextArea(TextArea 名称:infoLogTextArea)

  3. 然后只启动更新方法(update() 抛出异常)。

这是代码:

@FXML
    public void handleUpdateButton() {
        
        infoLogTextArea.setVisible(true);
        infoLogTextArea.appendText("Please wait while downloading data from internet.....\n");      
        plotButton.setDisable(true);
        updateButton.setDisable(true);
            
        if(c!=null) {
            Runnable task = new Runnable() {
                @Override
                public void run() {
                    // Thread.sleep(10000); -> sleep for 10secs
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                c.updateData();
                                infoLogTextArea.appendText(c.getErrorLog().toString());
                                plotLabel.setText(c.getCityData().size()+" cities found and updated from internet");
                                infoLogTextArea.appendText("Successfully updated the data from Internet\n");
                            }catch (IOException e) {
                                infoLogTextArea.setText("Couldnot update the data …
Run Code Online (Sandbox Code Playgroud)

java multithreading javafx runnable

2
推荐指数
2
解决办法
9571
查看次数

标签 统计

java ×1

javafx ×1

multithreading ×1

runnable ×1