场景:我想在此方法上实现该功能runTask()它应该在特定时间内出现,它应该花费浏览按钮上传xlv文件在数据库中的时间。
当我想显示进度条时,这是我的方法我在浏览按钮方法中调用此方法。
//Start of prograssbar method call
private void runTask() {
final double wndwWidth = 300.0d;
Label updateLabel = new Label("Running tasks...");
updateLabel.setPrefWidth(wndwWidth);
JFXProgressBar progress = new JFXProgressBar();
progress.setPrefWidth(wndwWidth);
VBox updatePane = new VBox();
updatePane.setPadding(new Insets(10));
updatePane.setSpacing(5.0d);
updatePane.getChildren().addAll(updateLabel, progress);
Stage taskUpdateStage = new Stage(StageStyle.UTILITY);
taskUpdateStage.setScene(new Scene(updatePane));
taskUpdateStage.show();
Task longTask = new Task<Void>() {
@Override
protected Void call() throws Exception {
int max = 100;
for (int i = 1; i <= max; i++) {
if (isCancelled()) {
break;
}
updateProgress(i, max);
updateMessage("Task part " + String.valueOf(i) + " complete");
Thread.sleep(10);
}
return null;
}
};
longTask.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent t) {
taskUpdateStage.hide();
}
});
progress.progressProperty().bind(longTask.progressProperty());
updateLabel.textProperty().bind(longTask.messageProperty());
taskUpdateStage.show();
new Thread(longTask).start();
}
**//end of prograssbar method**
下面是browseButtonClicked方法,用于加载文件并将其拆分并发送到数据库。这两种方法属于同一类。
private long count;
private CopyTask copyTask;
**//start of browseButton**
@FXML
private void browseButtonOnClicked() throws InterruptedException {
Stage primaryStage = new Stage();
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
fileChooser.getExtensionFilters().addAll(
new ExtensionFilter("Excel Files", "*.xlsm", "*.xlsx", "*.xlt", "*.xlv", "*.ods", "*.csv"));
File selectedFile = fileChooser.showOpenDialog(primaryStage);
count = selectedFile.length();
/*
* Im counting files length of selected files. and progress bar & indicator is showing
*/
if (selectedFile.getAbsolutePath() != null) {
filePathField.setText(selectedFile.getAbsolutePath());
}
// System.out.println(selectedFile.getAbsolutePath());
Scanner input = null;
try {
input = new Scanner(new File(selectedFile.getAbsolutePath()));
} catch (IOException e) {
}
/**
* Need To ask from the user whether he wants to remove his previous
* data or not if users proceed further all of the data would lost
*/
if (confirmMessage("It will erase all of your previous data\nDo you want to proceed further?")) {
**//this method is for progressbar call method**
runTask();
/**
* Here Need to execute a query which will erase all previous data.
* The data of pats, allocated students and unallocated students
* will be lost.
*/
if (patListButton.isSelected()) {
String deleteQuery = "TRUNCATE patsinformation;";
dataBase.executeQuery(deleteQuery);
listItems.clear();
pats.clear();
deleteQuery = "TRUNCATE studentsinformation;";
dataBase.executeQuery(deleteQuery);
deleteQuery = "TRUNCATE unallocatedstudents;";
dataBase.executeQuery(deleteQuery);
students.clear();
} else if (studentListButton.isSelected()) {
String deleteQuery = "TRUNCATE studentsinformation;";
dataBase.executeQuery(deleteQuery);
deleteQuery = "TRUNCATE unallocatedstudents;";
dataBase.executeQuery(deleteQuery);
students.clear();
}
while (input.hasNextLine()) {
if (patListButton.isSelected()) {
/**
* Here the under given query will erase of previous data.
* Delete Everything from pats information table.
*/
tokensPATInformation = input.nextLine().trim().split(",");
listItems.add(tokensPATInformation[1]);
pats.add(new PAT(tokensPATInformation[0], tokensPATInformation[1], tokensPATInformation[2], tokensPATInformation[3], tokensPATInformation[4], tokensPATInformation[5],
tokensPATInformation[6]));
String query = "INSERT INTO patsinformation values(" + '"' + tokensPATInformation[0] + '"'
+ "," + '"' + tokensPATInformation[1] + '"' + "," + '"' + tokensPATInformation[2] + '"'
+ "," + '"' + tokensPATInformation[3] + '"' + "," + '"' + tokensPATInformation[4] + '"'
+ "," + '"' + tokensPATInformation[5] + '"' + "," + '"'
+ tokensPATInformation[6] + '"' + ");";
dataBase.executeQuery(query);
}
if (studentListButton.isSelected()) {
String deleteQuery = "TRUNCATE studentsinformation;";
dataBase.executeQuery(deleteQuery);
deleteQuery = "TRUNCATE unallocatedstudents;";
dataBase.executeQuery(deleteQuery);
/**
* Here the under given query will erase of previous data.
* Delete Everything from students information and
* unallocated students table.
*/
String result = input.nextLine();
tokensStudentInformation = result.trim().split(",");
/**
* getting the data of each year student seperately to get
* hnadle the data easily. And to avoid using of loops again
* and again to find the data of a single year student. It
* is done because if auto allocattion is allowed then the
* system may able to select different students.
*/
if (tokensStudentInformation[3].trim().equalsIgnoreCase("Year-1")) {
year1Students.add(result);
} else if (tokensStudentInformation[3].trim().equalsIgnoreCase("year-2")) {
year2Students.add(result);
} else if (tokensStudentInformation[3].trim().equalsIgnoreCase("year-3")) {
year3Students.add(result);
} else if (tokensStudentInformation[3].trim().equalsIgnoreCase("year-4")) {
year4Students.add(result);
}
students.add(new Student(tokensStudentInformation[0], tokensStudentInformation[1],
tokensStudentInformation[2], tokensStudentInformation[3], tokensStudentInformation[4],
tokensStudentInformation[5]));
String query = "INSERT INTO unallocatedstudents values(" + '"' + tokensStudentInformation[0] + '"'
+ "," + '"' + tokensStudentInformation[1] + '"' + "," + '"' + tokensStudentInformation[2] + '"'
+ "," + '"' + tokensStudentInformation[3] + '"' + "," + '"' + tokensStudentInformation[4] + '"'
+ "," + '"' + tokensStudentInformation[5] + '"' + ");";
dataBase.executeQuery(query);
}
final TreeItem<AssignPAT.Student> root = new RecursiveTreeItem<AssignPAT.Student>(students, RecursiveTreeObject::getChildren);
tableView.setRoot(root);
tableView.setShowRoot(false);
tableView.getSelectionModel().setSelectionMode(
SelectionMode.MULTIPLE
);
listView.setItems(listItems);
}
}
}
谢谢!期待积极的回应!