【发布时间】:2019-10-09 19:34:44
【问题描述】:
当在 JavaFX 中单击按钮时,我正在尝试检索 XLS 文件并将其加载到 TableView 中。我正在使用 Task 类和 ExecutorService 来启动新线程。我需要阅读器类可重用,但 FileChooser 没有出现。
这是我编写一些并发代码的尝试。我想知道我做错了什么以及如何改进我的代码,因为一切都是事件驱动的?
控制器类代码
public void retrieveCustomersFromXLS() {
try {
loader.setOnSucceeded(workerStateEvent -> {
File file = null;
try {
file = loader.get();
} catch (Exception e) {
e.printStackTrace();
}
if (file != null && file.exists()) {
reader.setWorkingFile(file);
executor.submit(reader);
}
});
reader.setOnSucceeded(workerStateEvent1 -> {
Object[][] XLSFile = new Object[0][];
try {
XLSFile = reader.get();
} catch (Exception e) {
e.printStackTrace();
}
if (XLSFile != null) {
tableInterface.setEntries(XLSFile);
tableInterface.setEntryType("customers");
executor.submit(tableInterface);
}
});
tableInterface.setOnSucceeded(workerStateEvent2 -> {
customerList = FXCollections.observableArrayList(tableInterface.getCustomerEntries());
column_customerReference.setCellValueFactory(new PropertyValueFactory<customers, Integer>("customerReference"));
column_customerName.setCellValueFactory(new PropertyValueFactory<customers, String>("customerName"));
column_customerAddress.setCellValueFactory(new PropertyValueFactory<customers, String>("customerAddress"));
column_customerPost.setCellValueFactory(new PropertyValueFactory<customers, Integer>("customerPost"));
column_customerRegion.setCellValueFactory(new PropertyValueFactory<customers, String>("customerRegion"));
column_customerID_DDV.setCellValueFactory(new PropertyValueFactory<customers, String>("customerDDV"));
table_customerImports.setItems(customerList);
});
executor.submit(loader);
} catch (Exception e) {
e.printStackTrace();
}
}
阅读器类文件
@Override
protected File call() throws Exception {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Choose Excel file");
fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("Excel", "*.xlsx", "*.xls", "*.csv"));
File selectedFile = fileChooser.showOpenDialog(new Stage());
if (selectedFile != null) {
path = selectedFile.getAbsolutePath();
file = new File(path);
}
return file;
}
【问题讨论】:
-
只是我的建议,如果您不知道自己在做什么(而且您不知道),那么就不要编写并发代码。相反,只需为一个没有任何并发或执行器服务或任务或任何东西的线程编写,看看它是否可以接受。仅当它不执行时,才编写并发代码,但将其基于您已经拥有的工作实现。如果您不知道该怎么做,请粘贴非并发代码的 complete minimal reproducible example(没有 UI 表,因为它们与并发无关)并提出问题关于如何使其并发。
-
在我进行任何 UI 或后台处理更改之前,我有一个工作的非并发示例,它非常不可读并阻塞了 UI 线程。我很清楚我不知道自己在做什么,但我想学习如何正确地制作并发程序。基本上我需要知道的是如何链接任务。
-
“基本上我需要知道的是如何链接任务”。您可以使用single thread executor。请参阅How to reset progress indicator between tasks in JavaFX2? 的答案。有关更简洁的示例,请参阅:How to queue tasks in JavaFX?
标签: java javafx concurrency