background: JavaFX's Project, in the previous article, said that the amount of data processed is relatively large, so the calculation process is very slow and takes about two minutes to compute. Process is locked, there is no response, and the calculation progress cannot be displayed.
Solution: Open a thread calculation, other features are still available, and then update the interface in a non-UI thread to show the calculation progress.
specific scenario: write a class, inherit thread, and then start (Note: Here you can pass the results in the thread via the Get method in the Class), here are three ways to update the UI in a non-UI thread
(1) JavaFX provides a function that can update the interface in a non-UI thread, and if you try to update the interface directly in the process, you will report an exception (pro-Test useful).
Platform.runlater (()->schedulelabel.settext ("0%"));
Note: Lambda expressions are used here
(2) The timeline class provided by JavaFX (no parental test, found this method)
New Timeline (New Keyframe (
duration.millis (2500),
ae-> dosomething ()))
. Play ();
(3) JavaFX ProgressBar class, which is essentially a task-updated UI, so it can be called a task method (pro-test feasible)
The public task Createworker () {The Return
New task () {
@Override
protected Object call () throws Exception {for
(int i = 0; i < i++) {
thread.sleep (); updatemessage ("milliseconds");
UpdateProgress (i + 1);
}
return true;
}
;
}
Call
Task Copyworker = Createworker ();
Progressbar.progressproperty (). Unbind (); Progressbar.progressproperty (). Bind (Copyworker.progressproperty ());
PostScript: I later tried to update the UI interface in a completely possessive way, with the above three ways of adding a thread directly to the task, failed. You should not be able to do anything else without fully seizing the process.
reference materials:
Method One, Platform.runlater (): https://stackoverflow.com/questions/13351959/ javafx-how-to-set-default-active-control/13352107#13352107
JavaFX ProgressBar progress bar: http://www.yiibai.com/ javafx/javafx_progressbar.html
Timeline: https://zhidao.baidu.com/question/1988620878344183347.html