在JavaFX的开发中,在其他线程更新JavaFX的线程上的信息的时候,就可能报这种错误

Not on FX application thread; currentThread = *

具体解决的方案:
找到子线程中更新主线程的代码
将其放在以下代码块中即可解决

Platform.runLater(new Runnable() {
@Override
public void run() {
//更新JavaFX的主线程的代码放在此处
}
});
1
2
3
4
5
6
例子:

源自:http://stackoverflow.com/questions/15174395/update-progress-bar-and-multiple-labels-from-thread

package test;

/**

  • Created by zxm on 2017/3/14.
    /
    import java.util.Arrays;
    import java.util.List;
    import static javafx.application.Application.launch;
    import javafx.application.
    ;
    import javafx.beans.value.;
    import javafx.concurrent.Task;
    import javafx.event.
    ;
    import javafx.scene.;
    import javafx.scene.control.
    ;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;

public class Mp3Finder extends Application {
final Label status = new Label();
final Label folderCount = new Label();
final Label fileCount = new Label();
final Label mp3Count = new Label();

@Override public void start(Stage stage) {
    final GridPane finderResults = new GridPane();
    finderResults.setPrefWidth(400);
    finderResults.setVgap(10);
    finderResults.setHgap(10);
    finderResults.addRow(0, new Label("Status: "),    status);
    finderResults.addRow(1, new Label("# Folders: "), folderCount);
    finderResults.addRow(2, new Label("# Files: "),   fileCount);
    finderResults.addRow(3, new Label("# mp3s: "),    mp3Count);

    final Button finderStarter = new Button("Find mp3s");
    finderStarter.setOnAction(new EventHandler() {
        @Override public void handle(ActionEvent t) {
            startMp3Finder(finderStarter);
        }
    });

    VBox layout = new VBox(10);
    layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10; -fx-font-size: 16;");
    layout.getChildren().setAll(finderStarter, finderResults);
    stage.setScene(new Scene(layout));
    stage.show();
}

private void startMp3Finder(final Node starterNode) {
    starterNode.setDisable(true);

    Mp3FinderTask task = new Mp3FinderTask(status, folderCount, mp3Count);
    task.runningProperty().addListener(new ChangeListener() {
        @Override public void changed(ObservableValue ov, Boolean wasRunning, Boolean isRunning) {
            if (!isRunning) {
                starterNode.setDisable(false);
            }
        }
    });

    final Thread thread = new Thread(task , "mp3-finder");
    thread.setDaemon(true);
    thread.start();
}

private class Mp3FinderTask extends Task> {
    private final Label status;
    private final Label folderCount;
    private final Label mp3Count;

    public Mp3FinderTask(Label status, Label folderCount, Label mp3Count) {
        this.status = status;
        this.folderCount = folderCount;
        this.mp3Count = mp3Count;
    }

    @Override protected List call() throws Exception {
        initFinderResults();

        updateLabelLater(status, "Finding Folders");
        setProgressIndicator(folderCount);
        List folders = findFolders();
        updateLabelLater(folderCount, folders.size() + "");

        updateLabelLater(status, "Finding Files");
        setProgressIndicator(fileCount);
        List files = findFiles(folders);
        updateLabelLater(fileCount, files.size() + "");

        updateLabelLater(status, "Find mp3s");
        setProgressIndicator(mp3Count);
        List mp3s = findMp3s(files);
        updateLabelLater(mp3Count, mp3s.size() + "");

        updateLabelLater(status, "All mp3s Found");

        return mp3s;
    }

    void updateLabelLater(final Label label, final String text) {
        Platform.runLater(new Runnable() {
            @Override public void run() {
                label.setGraphic(null);
                label.setText(text);
            }
        });
    }

    private List findFolders() throws InterruptedException {
        // dummy implementation
        Thread.currentThread().sleep(1000);
        return Arrays.asList("folder1", "folder2", "folder3");
    }

    private List findFiles(List folders) throws InterruptedException {
        // dummy implementation
        Thread.currentThread().sleep(1000);
        return Arrays.asList("file1", "file2", "file3", "file4", "file5");
    }

    private List findMp3s(List files) throws InterruptedException {
        // dummy implementation
        Thread.currentThread().sleep(1000);
        return Arrays.asList("music1", "music2");
    }

    private void initFinderResults() {
        Platform.runLater(new Runnable() {
            @Override public void run() {
                status.setText("");
                folderCount.setText("");
                fileCount.setText("");
                mp3Count.setText("");
            }
        });
    }

    private void setProgressIndicator(final Label label) {
        Platform.runLater(new Runnable() {
            @Override public void run() {
                label.setGraphic(new ProgressIndicator());
            }
        });
    }
}

public static void main(String[] args) { launch(args); }

}


作者:商角徵羽宫
来源:CSDN
原文:https://blog.csdn.net/zxm1306192988/article/details/62044352
版权声明:本文为博主原创文章,转载请附上博文链接!

发表评论

邮箱地址不会被公开。 必填项已用*标注