1 回答
TA贡献1851条经验 获得超5个赞
您必须调用JavaFX 应用程序线程FileChooser#showXXXDialog上的方法。如果您发现任务失败,您将看到一条消息,说明您尝试在错误的线程上执行操作。此外,您不需要后台任务来提示用户输入文件。IllegalStateException
Task下面是提示用户输入文本文件、读取 a 中的文本文件并将结果放入 a 中的示例ListView。
App.java
package com.example;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class App extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
Scene scene = new Scene(FXMLLoader.load(getClass().getResource("/App.fxml")));
primaryStage.setScene(scene);
primaryStage.setTitle("FileChooser Example");
primaryStage.show();
}
}
应用程序.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.layout.VBox?>
<VBox xmlns="http://javafx.com/javafx/13" xmlns:fx="http://javafx.com/fxml/1" spacing="5" prefWidth="600"
prefHeight="400" alignment="CENTER" fx:controller="com.example.Controller">
<padding>
<Insets topRightBottomLeft="5"/>
</padding>
<Button text="Open File..." onAction="#handleOpenFile"/>
<Separator/>
<ListView fx:id="listView" VBox.vgrow="ALWAYS"/>
</VBox>
Controller.java
package com.example;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import javafx.collections.FXCollections;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.ListView;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
public class Controller {
private final Executor executor = Executors.newSingleThreadExecutor(r -> {
Thread t = new Thread(r, "controller-thread");
t.setDaemon(true);
return t;
});
@FXML private ListView<String> listView;
@FXML
private void handleOpenFile(ActionEvent event) {
event.consume();
FileChooser chooser = new FileChooser();
chooser.getExtensionFilters()
.add(new ExtensionFilter("Text Files", "*.txt", "*.json", "*.xml", "*.html", "*.java"));
File file = chooser.showOpenDialog(listView.getScene().getWindow());
if (file != null) {
ReadFileTask task = new ReadFileTask(file.toPath());
task.setOnSucceeded(wse -> listView.setItems(FXCollections.observableList(task.getValue())));
task.setOnFailed(wse -> task.getException().printStackTrace());
executor.execute(task);
}
}
private static class ReadFileTask extends Task<List<String>> {
private final Path file;
private ReadFileTask(Path file) {
this.file = Objects.requireNonNull(file);
}
@Override
protected List<String> call() throws Exception {
return Files.readAllLines(file);
}
}
}
添加回答
举报