我目前正在学习JavaFX。我正在构建一个应用程序,它将文件作为输入,连接到外部服务器,并将解析后的结果返回到MySQL数据库。核心功能工作正常,但我希望对用户进行JavaFX更新,以便他们知道他们正在等待连接到服务器等。但是,当调用该方法时,文本更新在方法之前是不可见的已完成执行 - 我希望更新实时发生。违规方法如下:
public void doBlast(){
//this line is not updated until completion of the program - not straight away
status.setText("Connecting to NCBI servers..NCBI" + "\n");
//Biojava API details extracted as it is not relevant
BufferedReader bufferedFileReader = null;
Scanner bufferedScanner = null;
BufferedWriter bufferedWriterXML = null;
String rid = null;
int count = 0;
try {
//this.getFasta() represents the input file
bufferedScanner
= new Scanner(new BufferedReader(new FileReader (this.getFasta())));
//this.getTempXML() is a temporary file for temporary storage of xml output from the server
bufferedWriterXML
= new BufferedWriter (new FileWriter (this.getTempXML(), true));
String line, nextline, query, xmlLine;
//Loops for a specified number of times
while (count < alignmentLimit) {
this.setSequence_id(bufferedScanner.nextLine());
//newline here
bufferedScanner.nextLine();
//this line from the file is used to search a database in the server
query = bufferedScanner.nextLine();
//this line is not updated until completion of the program - not straight away
status.setText("Connecting to NCBI servers.." + "\n");
//below service connects to server
rid = service.sendAlignmentRequest(query, props);
//results
InputStream in = service.getAlignmentResults(rid, outputProps);
//this line is not updated until completion of the program - not straight away
status.setText("Connected to NCBI." + "\n");
bufferedFileReader = new BufferedReader(new InputStreamReader(in));
xmlLine = bufferedFileReader.readLine();
while (xmlLine != null) {
bufferedWriterXML.write("\n" + xmlLine);
xmlLine = bufferedFileReader.readLine();
// have to flush here so that it prints to file;
// the file can then be deleted each loop
bufferedWriterXML.flush();
}
count++;
//this method removes specific strings
this.removeAll("<?xml", "<!DOCTYPE", this.getTempXML(), this.getXML());
//this method fills a database with the data from server
this.xml2mysql();
//file cleaning
this.eraseData(this.getTempXML());
this.eraseData(this.getXML());
bufferedScanner.nextLine();
//this line is not updated until completion of the program - not straight away
numberOfRecords.setText(Integer.toString(count));
}
}
catch (Exception anException){
anException.printStackTrace();
}
finally {
try {
bufferedWriterXML.close();
bufferedScanner.close();
bufferedFileReader.close();
}
catch (Exception anException){
System.out.println("Error: " + anException);
}
}
}
setText()方法使用FXML定义的fx:id变量,上面的方法在控制器中。
非常感谢有人能帮助我。我到处寻找解决方案。 感谢。
答案 0 :(得分:0)
您应该查看使用将文件加载到后台线程的任务或服务。
您可以挂钩执行期间调用的方法:
来自Oracle文档:
在call方法中,你可以使用updateProgress,updateMessage, updateTitle方法,用于更新相应的值 JavaFX应用程序线程上的属性。但是,如果任务是 取消后,将忽略来自call方法的返回值
如果在JavaFX线程上执行长进程,UI将被阻止并显示为冻结。