javafx--non-UI thread interface update problem

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.