我正在尝试填充TreeView并在填充之前和之后更新状态标签的文本。
我的代码如下:
public void populateTreeView() {
// start message
this.label.setText("Process started...");
// populate the TreeView, this takes several minutes
// finish message
this.label.setText("Done!");
}
它没有按我预期的那样工作。我尝试将该方法的3个主要部分分别放在Platform.runLater()中,但也没有用。该应用程序冻结了几秒钟,然后我看到填充的TreeView和“完成”文本。
如何获得这种行为?
预先感谢
答案 0 :(得分:1)
您将需要在后台线程中执行加载方法。基本上,您创建的接口及其所有事件在JavaFX Application Thread(JFXAT)上执行。
这些事件通常一次执行一次,因此,如果您在此线程上运行一个较长的进程,则该用户界面似乎会冻结,直到该进程完成为止。
虽然有多种方法可以在JavaFX中创建后台任务,但下面是一个简单的演示应用程序,该应用程序使用Task
进行操作。
示例代码在全文中都有注释,以解释我们在做什么。为了简单起见,该示例使用ListView
代替TreeView
,但是无论如何,概念都是相同的。
此示例显示了一个基本界面,其中包含一个ListView
和一个Button
,以启动加载过程。底部是Label
,它将使用户了解任务过程中的当前步骤。
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.concurrent.Task;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.List;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// Just a simple interface
VBox root = new VBox(5);
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);
// Create the list view
ListView<String> listView = new ListView<>();
listView.setPrefHeight(200);
// Status label
Label lblStatus = new Label();
// Here we will create a new Task that will run in the background
Task<List<String>> loadDataTask = new Task<List<String>>() {
// We override the call() method; this is the code that is run when the task is started. When the Task
// is completed, it will return a new List of Strings
@Override
protected List<String> call() throws Exception {
// Tasks have a messageProperty that allows us to keep the UI informed. We will bind this value to
// our Label later
updateMessage("Loading data ...");
// Generate some sample data
List<String> listOfItems = new ArrayList<>();
listOfItems.add("One");
listOfItems.add("Two");
listOfItems.add("Three");
listOfItems.add("Four");
listOfItems.add("Five");
// Simulate a long-running task by pausing the thread for 10 seconds
Thread.sleep(10000);
// Now we can update the messageProperty again and return the completed data List
updateMessage("Finished!");
return listOfItems;
}
};
// We can now tell our application what to do once the Task has finished (either successfully or failure)
loadDataTask.setOnFailed(wse -> {
// This is called if an exception is thrown during the execution of the Task
// We will just print the Exception in this sample
loadDataTask.getException().printStackTrace();
});
// The Task completed successfully so lets now bind the List to our ListView
loadDataTask.setOnSucceeded(wse -> {
listView.setItems(FXCollections.observableArrayList(loadDataTask.getValue()));
});
// Now that we've defined our background Task and what to do when it's completed, let's create a button
// that allows us to start the task.
Button button = new Button("Load Data");
button.setOnAction(e -> {
// Let's bind our Label to the Task's messageProperty. This will cause the Label to update automatically
// whenever the Task calls the updateMessage() method.
lblStatus.textProperty().bind(loadDataTask.messageProperty());
// Now let's start the Task on a background Thread. This will cause the rest of the UI to remain
// responsive.
new Thread(loadDataTask).start();
});
// Add the controles to the Scene
root.getChildren().addAll(
button,
listView,
new Label("Status:"),
lblStatus);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}
单击按钮后,将执行后台任务,Label
将更新为显示“正在加载数据...”,并且长时间运行的任务将开始。
任务完成后,ListView
将使用数据进行更新,而Label
将显示“完成!”