Java notes-Use the Swingwoker class to complete time-consuming operations

Source: Internet
Author: User

Use the Swingwoker class to complete the time-consuming operation:

For time-consuming operations in swing, it is common to run in a new thread to prevent the program from "Suspended animation."
In java6.0, you can use the Swingwoker class to complete

swingwoker<t,v> is an abstract class that performs long-time GUI interaction tasks in a dedicated thread. With swing
There are two constraints to keep in mind when writing a multithreaded application:
1. You should not run time-consuming tasks on the event assignment thread, or the application will not respond.
2. The swing control can only be accessed on the event assignment thread
Note: It is often necessary to place time-consuming tasks in the Doinbackground () method of the Swingwoker class
Perform:
protected abstract T Doinbackground () throws Exception

--If you want to reprint this article please indicate the reprint address "http://www.cnblogs.com/XHJT/p/3912080.html" Thank you--

code example:

 PackageCom.xhj.thread;ImportJava.awt.Button;ImportJava.awt.Label;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;Importjava.util.Arrays;ImportJava.util.Random;ImportJavax.swing.JFrame;ImportJavax.swing.JPanel;ImportJavax.swing.SwingWorker;/** * @authorXiehejun **/ Public classUsedswingwokerthreadextendsJFrame {PrivateLabel lable; Privatebutton button;  PublicLabel getlable () {returnlable; }     Public voidsetlable (Label lable) { This. lable =lable; }     PublicButton Getbutton () {returnbutton; }     Public voidSetbutton (Button button) { This. Button =button; }    Private classNumberarraryextendsSwingworker<void, integer>{@OverrideprotectedVoid Doinbackground ()throwsException {Button.addactionlistener (NewActionListener () {@Override Public voidactionperformed (ActionEvent e) {int[] Numarrary =New int[1000];  for(inti = 0; i < 1000; i++) {Numarrary[i]=NewRandom (). Nextint (); System.out.println (i+ "Numbered:" +Numarrary[i]);                    } arrays.sort (Numarrary); System.out.println ("The maximum number of random numbers generated is:" + numarrary[numarrary.length-1]); Lable.settext ("The maximum number of random numbers generated is:" + numarrary[numarrary.length-1]);            }            }); return NULL; }    }    /**     * @paramargs*/     Public Static voidMain (string[] args) {usedswingwokerthread Usedswingwoker=NewUsedswingwokerthread (); JFrame Frame=NewJFrame (); JPanel Panel=NewJPanel ();        Frame.setcontentpane (panel); Frame.setvisible (true); Usedswingwoker.setlable (NewLabel ("click button to generate a random array"))); Usedswingwoker.setbutton (NewButton ("Start Build"));        Frame.add (Usedswingwoker.getlable ());        Frame.add (Usedswingwoker.getbutton ()); Numberarrary Arrarys= Usedswingwoker.Newnumberarrary (); Try{arrarys.doinbackground (); } Catch(Exception e) {e.printstacktrace (); }    }}

Note: Swingwoker Life cycle and thread
1. Current thread: Call the Execute () method on the thread. It dispatches Swingwoker to execute on the worker thread and returns immediately.
You can use the Get method to wait for Swingwoker to complete.
2.Worker Thread: Call the Doinbackground () method on the thread. All background activity should occur on this thread.
To notify Propertychanglisteners about changes to the binding (bound) attribute, use the Firepropertychang
and Getpropertychangesupport () method, by default, there are two binding properties available, namely State and progress
3. Event Assignment Thread: all swing-related activities occur on that thread. Swingwoker calls the process and done () methods,
and notifies all propertychanglistener of the thread.

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.