我在“提交”按钮上有两个事件处理程序。当前,一个处理程序读取一个excel文件,将其发送到Web服务,然后返回结果,并将地址和结果写入一个新的excel文件。使用我的其他处理程序时,我希望它在执行第一个处理程序时在场景顶部显示进度指示器,但是它不执行此操作,它仅执行第一个处理程序(WriteExcel),而不执行进度指示器。仅当我要删除WriteExcel事件处理程序时,进度指示器才会起作用。
我对Java有一定的了解,但是我对JavaFX还是陌生的,因此我目前在尝试使此进度指示器与ExcelWriter协同工作时遇到一些麻烦。
这是我的代码:
public class SubmitControls extends BorderPane {
private static VBox submit;
private static Labeled fileSelectTxt;
private static VBox progressBox;
private static StackPane stack;
public SubmitControls() {
// Submit UI controls
stack = new StackPane();
// File Check text
final Text checkTxt = new Text("Is this the file you want to be checked?");
checkTxt.setFill(Color.BLACK);
checkTxt.setFont(Font.font("Arial", FontWeight.MEDIUM, 22));
// File Selected Text Display
fileSelectTxt = new Label();
fileSelectTxt.setFont(Font.font("Arial", FontWeight.BOLD, 18));
fileSelectTxt.setTextFill(Color.BLACK);
// Check Connectivity Button
final Button checkBtn = new Button("Check Connectivity");
checkBtn.setCursor(Cursor.HAND);
//checkBtn.setOnAction(new WriteExcel());
checkBtn.addEventHandler(ActionEvent.ACTION, new WriteExcel());
checkBtn.addEventHandler(ActionEvent.ACTION, event -> {
ProgressIndicator progress = new ProgressIndicator();
progressBox = new VBox(progress);
progressBox.setAlignment(Pos.CENTER);
// Grey Background
submit.setDisable(true);
stack.getChildren().add(progressBox);
});
// Reselect Button
final Button reselectBtn = new Button("Reselect File");
reselectBtn.setOnAction(new FileSelector());
reselectBtn.setCursor(Cursor.HAND);
// Configure the submit VBox
submit = new VBox();
submit.getChildren().addAll(checkTxt, fileSelectTxt, checkBtn, reselectBtn);
submit.setAlignment(Pos.CENTER);
submit.setSpacing(25);
submit.getStyleClass().add("vbox");
stack.getChildren().add(submit);
setCenter(stack);
}
这是我的WriteExcel活动:
public class WriteExcel implements EventHandler<ActionEvent> {
static String home = System.getProperty("user.home");
static String fileName = "Results";
private static final String path = home + "/Downloads/" + fileName + ".xlsx";
private void writeExcel(String pathToExcel, List<AddressDetails> addresses, List<Results> results) {
final String[] header = { "Building Name", "Building Number", "Street Name", "City", "PostCode", "Country",
"Results" };
Workbook workbook = null;
try {
workbook = new XSSFWorkbook();
;
// Creating sheet within the workbook
Sheet sheet = workbook.createSheet("Address Results");
// For Header
Font font = workbook.createFont();
font.setFontName("Calibri");
font.setColor(IndexedColors.BLACK.getIndex());
font.setBold(true);
font.setFontHeightInPoints((short) 12);
Font font2 = workbook.createFont();
font2.setFontName("Calibri");
font2.setColor(IndexedColors.WHITE.getIndex());
font2.setBold(true);
font2.setFontHeightInPoints((short) 14);
// Cell style for cell headers
CellStyle style = workbook.createCellStyle();
style.setFont(font);
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setBorderRight(BorderStyle.THIN);
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderLeft(BorderStyle.THIN);
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderTop(BorderStyle.THIN);
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderBottom(BorderStyle.THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
Row row = sheet.createRow(1);
for (int i = 0; i < header.length; i++) {
sheet.setDefaultColumnWidth(20);
Cell cell = row.createCell(i);
cell.setCellValue(header[i]);
cell.setCellStyle(style);
}
// Cell style for title
CellStyle style2 = workbook.createCellStyle();
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
style2.setFont(font2);
style2.setFillForegroundColor(IndexedColors.ROYAL_BLUE.getIndex());
style2.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style2.setAlignment(HorizontalAlignment.CENTER);
style2.setVerticalAlignment(VerticalAlignment.CENTER);
// Title Cell
Row row1 = sheet.createRow(0);
Cell cell1 = row1.createCell(0);
cell1.setCellValue("Address Results");
cell1.setCellStyle(style2);
// What Row in the sheet addresses fill up from
int rowNum = 2;
for (AddressDetails address : addresses) {
// create new row
row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(address.getBuildName());
row.createCell(1).setCellValue(address.getBuildNum());
row.createCell(2).setCellValue(address.getStreetName());
row.createCell(3).setCellValue(address.getCity());
row.createCell(4).setCellValue(address.getPostCode());
row.createCell(5).setCellValue(address.getCountry());
}
int rowNumb = 2;
for (Results result : results) {
row = sheet.getRow(rowNumb++);
row.createCell(6).setCellValue(result.getResult());
}
// Writing sheet data
FileOutputStream outputStream = new FileOutputStream(pathToExcel);
workbook.write(outputStream);
System.out.println("Complete");
} catch (EncryptedDocumentException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (workbook != null)
workbook.close();
InterfaceLayout.getRoot().setCenter(Map.getMapDisplay());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void handle(ActionEvent event){
WriteExcel excelWriter = new WriteExcel();
List<AddressDetails> addresses = ReadExcel.readExcel();
List<Results> results = null;
try {
results = CheckRequest.getResults();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
excelWriter.writeExcel(path, addresses, results);
}
}