I'm trying to retrieve an XLS file and load it into a TableView when a button is clicked in JavaFX. I'm using Task class and ExecutorService to launch new threads. I need the reader class to be reusable, but the FileChooser isn't showing up.
This is my attempt at writing some concurrent code. I'd like to know what am I doing wrong and how would I improve my code, since everything is event-driven?
Controller class code
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();
}
}
Reader class file
@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;
}