概述
在JavaFX的开发中,在其他线程更新JavaFX的线程上的信息的时候,就可能报这种错误
Not on FX application thread; currentThread = *
具体解决的方案:
找到子线程中更新主线程的代码
将其放在以下代码块中即可解决
Platform.runLater(new Runnable() {
@Override
public void run() {
//更新JavaFX的主线程的代码放在此处
}
});
例子:
源自: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<ActionEvent>() {
@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<Boolean>() {
@Override public void changed(ObservableValue<? extends Boolean> 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<List<String>> {
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<String> 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<String> findFolders() throws InterruptedException {
// dummy implementation
Thread.currentThread().sleep(1000);
return Arrays.asList("folder1", "folder2", "folder3");
}
private List<String> findFiles(List<String> folders) throws InterruptedException {
// dummy implementation
Thread.currentThread().sleep(1000);
return Arrays.asList("file1", "file2", "file3", "file4", "file5");
}
private List<String> findMp3s(List<String> 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); }
}
最后
以上就是干净镜子为你收集整理的javaFX 子线程更新主线程UI的全部内容,希望文章能够帮你解决javaFX 子线程更新主线程UI所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复