Javafx将标签绑定到StringProperty的位置
我已经为此苦苦挣扎了好几天,我读过关于线程、MVC、绑定、接口和许多有趣的东西,但我只是不能用适当的方式将它们组合在一起来实现这一点 我只想列出mi c:\中的所有文件,并将它们显示在不断变化的标签上 但我得到的只是:Javafx将标签绑定到StringProperty的位置,java,javafx,fxml,java-threads,Java,Javafx,Fxml,Java Threads,我已经为此苦苦挣扎了好几天,我读过关于线程、MVC、绑定、接口和许多有趣的东西,但我只是不能用适当的方式将它们组合在一起来实现这一点 我只想列出mi c:\中的所有文件,并将它们显示在不断变化的标签上 但我得到的只是: Exception in thread "Thread-4" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4 这是我的FXML: <?impor
Exception in thread "Thread-4" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4
这是我的FXML:
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane alignment="center" hgap="10" prefHeight="200.0" prefWidth="401.0" vgap="10" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60" fx:controller="sample.Controller">
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
</rowConstraints>
<children>
<AnchorPane prefHeight="200.0" prefWidth="368.0">
<children>
<Button fx:id="start" layoutX="159.0" layoutY="35.0" mnemonicParsing="false" onAction="#displayFiles" text="Start" />
<Label fx:id="fileLabel" layoutX="20.0" layoutY="100.0" prefHeight="21.0" prefWidth="329.0" text="This label must change on iteration" />
</children>
</AnchorPane>
</children>
</GridPane>
我的控制器:
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
public class Controller {
@FXML
Button start;
@FXML
Label fileLabel;
@FXML
void displayFiles(ActionEvent event) throws Exception{
Model model = new Model();
//BINDING
fileLabel.textProperty().bind(model.status);
Thread thread = new Thread(model);
thread.start();
}
}
模型:
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import java.io.File;
/**
* Created by R00715649 on 16-Nov-16.
*/
public class Model implements Runnable {
File rootDirectory = new File("C:/");
StringProperty status = new SimpleStringProperty("Starting scan...");
@Override
public void run() {
try{
File[] fileList = rootDirectory.listFiles();
for (File f:fileList){
processDirectory(f);
}
}catch (Exception e){
}
}
void processDirectory (File directory){
if (directory.isDirectory()){
File[] fileList = directory.listFiles();
for (File f:fileList){
processDirectory(f);
}
}else{
System.out.println(directory.getAbsolutePath());
status.set(directory.getAbsolutePath());
}
}
}
应该在主线程中。应该是这样的:
Platform.runLater(() -> status.set(directory.getAbsolutePath()));
应该在主线程中。应该是这样的:
Platform.runLater(() -> status.set(directory.getAbsolutePath()));
由于标签的文本绑定到模型的状态,因此更改模型的状态将导致UI中的更改(标签的文本将更改)。因此,您只能在FX应用程序线程上更改模型的状态 您可以使用
Platform.runLater(…)
计划在FX应用程序线程上运行代码。您可以直接在模型中执行此操作:
void processDirectory (File directory){
if (directory.isDirectory()){
File[] fileList = directory.listFiles();
for (File f:fileList){
processDirectory(f);
}
}else{
System.out.println(directory.getAbsolutePath());
Platform.runLater(() -> status.set(directory.getAbsolutePath()));
}
}
或者,您可以使用模型的状态(而不是绑定)注册侦听器,并将其委托给FX应用程序线程:
@FXML
void displayFiles(ActionEvent event) throws Exception{
Model model = new Model();
ChangeListener<String> listener = (obs, oldStatus, newStatus) -> fileLabel.setText(newStatus);
model.status.addListener(listener);
Thread thread = new Thread(model);
thread.start();
}
由于标签的文本绑定到模型的状态,因此更改模型的状态将导致UI中的更改(标签的文本将更改)。因此,您只能在FX应用程序线程上更改模型的状态 您可以使用
Platform.runLater(…)
计划在FX应用程序线程上运行代码。您可以直接在模型中执行此操作:
void processDirectory (File directory){
if (directory.isDirectory()){
File[] fileList = directory.listFiles();
for (File f:fileList){
processDirectory(f);
}
}else{
System.out.println(directory.getAbsolutePath());
Platform.runLater(() -> status.set(directory.getAbsolutePath()));
}
}
或者,您可以使用模型的状态(而不是绑定)注册侦听器,并将其委托给FX应用程序线程:
@FXML
void displayFiles(ActionEvent event) throws Exception{
Model model = new Model();
ChangeListener<String> listener = (obs, oldStatus, newStatus) -> fileLabel.setText(newStatus);
model.status.addListener(listener);
Thread thread = new Thread(model);
thread.start();
}