如何在我的案例中使用 javaFX Task - 不重复
How to use javaFX Task in my case - Not duplicate
我知道我的问题有答案,但我不明白我的代码中的问题。
为什么我得到:
java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4
我正在尝试使用控制器 class 中的方法将任务中的文本添加到文本流中,由于某种原因程序在 .getChildren() 方法上失败。
在控制器中调用 Spliter class:
btnSplit.setOnMouseClicked(new EventHandler<Event>() {
@Override
public void handle(Event event) {
Thread split = new Thread(new Spliter(custToSplit, Controller.this));
split.setDaemon(true);
split.start();
}
});
class 拆分器构造函数:
public Spliter(File f, Controller c){
this.c = c;
this.cust = f;
}
c.updateHebFlow("Dir created: "+ newDir.getAbsolutePath() , INFO_TEXT);
控制器的一部分class:
@FXML
private TextFlow hebFlow;
@Override
public void initialize(URL location, ResourceBundle resources) {
assert hebFlow != null : "fx:id=\"hebFlow\" was not injected: check your FXML file 'MainXml.fxml'.";
public void updateHebFlow(String text,String type){
normalText = new Text();
errorText = new Text();
errorText.setFill(Color.RED);
infoText = new Text();
infoText.setFill(Color.BLUE);
switch(type){
case(ERROR_TEXT) :
errorText.setText(text);
hebFlow.getChildren().addAll(new Text("/n"), errorText);
break;
case(INFO_TEXT) :
infoText.setText(text);
hebFlow.getChildren().addAll(new Text("/n"), infoText);
break;
case(NORMAL_TEXT) :
normalText.setText(text);
hebFlow.getChildren().addAll(new Text("/n"), normalText);
break;
}
}
}
在 Spliter 中调用 updateHebFlow class:
try{
c.updateHebFlow("Script by TulTul", INFO_TEXT);
}catch (Exception e){
e.printStackTrace();
}
据我所知,除了控制器之外,我无法从其他 class 更改 UI,因此我在控制器 class 中创建了一个方法,该方法将进行更改并调用它在任务 class 中,为什么会出现此异常?
如果这是错误的,正确的方法是什么?
From what I understand I cant change the UI from other class other then the controller
其实正确的说法是:"You cannot change the UI from any thread other than the JavaFX UI thread"。因此,解决方案是使用 Splitter
中的 Platform.runLater()
作为:
// Java 8
Platform.runLater(() -> {
c.updateHebFlow("Script by TulTul", INFO_TEXT);
});
// Java 7
Platform.runLater(new Runnable() {
public void run() {
c.updateHebFlow("Script by TulTul", INFO_TEXT);
}
});
Platform.runLater()
保证 运行 JavaFX UI 线程中的块和调用顺序。
我知道我的问题有答案,但我不明白我的代码中的问题。
为什么我得到:
java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4
我正在尝试使用控制器 class 中的方法将任务中的文本添加到文本流中,由于某种原因程序在 .getChildren() 方法上失败。
在控制器中调用 Spliter class:
btnSplit.setOnMouseClicked(new EventHandler<Event>() {
@Override
public void handle(Event event) {
Thread split = new Thread(new Spliter(custToSplit, Controller.this));
split.setDaemon(true);
split.start();
}
});
class 拆分器构造函数:
public Spliter(File f, Controller c){
this.c = c;
this.cust = f;
}
c.updateHebFlow("Dir created: "+ newDir.getAbsolutePath() , INFO_TEXT);
控制器的一部分class:
@FXML
private TextFlow hebFlow;
@Override
public void initialize(URL location, ResourceBundle resources) {
assert hebFlow != null : "fx:id=\"hebFlow\" was not injected: check your FXML file 'MainXml.fxml'.";
public void updateHebFlow(String text,String type){
normalText = new Text();
errorText = new Text();
errorText.setFill(Color.RED);
infoText = new Text();
infoText.setFill(Color.BLUE);
switch(type){
case(ERROR_TEXT) :
errorText.setText(text);
hebFlow.getChildren().addAll(new Text("/n"), errorText);
break;
case(INFO_TEXT) :
infoText.setText(text);
hebFlow.getChildren().addAll(new Text("/n"), infoText);
break;
case(NORMAL_TEXT) :
normalText.setText(text);
hebFlow.getChildren().addAll(new Text("/n"), normalText);
break;
}
}
}
在 Spliter 中调用 updateHebFlow class:
try{
c.updateHebFlow("Script by TulTul", INFO_TEXT);
}catch (Exception e){
e.printStackTrace();
}
据我所知,除了控制器之外,我无法从其他 class 更改 UI,因此我在控制器 class 中创建了一个方法,该方法将进行更改并调用它在任务 class 中,为什么会出现此异常? 如果这是错误的,正确的方法是什么?
From what I understand I cant change the UI from other class other then the controller
其实正确的说法是:"You cannot change the UI from any thread other than the JavaFX UI thread"。因此,解决方案是使用 Splitter
中的 Platform.runLater()
作为:
// Java 8
Platform.runLater(() -> {
c.updateHebFlow("Script by TulTul", INFO_TEXT);
});
// Java 7
Platform.runLater(new Runnable() {
public void run() {
c.updateHebFlow("Script by TulTul", INFO_TEXT);
}
});
Platform.runLater()
保证 运行 JavaFX UI 线程中的块和调用顺序。