This article gives examples of using the SwingWorker class. The purpose of the SwingWorker class is to implement a background thread that allows you to perform some time-consuming operations without affecting the performance of your program's GUI. For some basic information about the SwingWorker class, see "Threads and SWING".
The thread is a basic solution to the Swing program that performs some fees or blockable operations. For example, if your application wants to send a database request or load a file according to the menu item selected by the user, then you should do this in a separate thread. This article describes the ways to complete the above work in an isolated Worker thread. This article includes the following main contents:
SwingWorker Class: This section tells you how to download the SwingWorker class and describe the use of the SwingWorker class. Introduced the interrupt () method of the SwingWorker class. Examples of introducing a Worker thread: Demos an application that uses the SwingWorker class. Example 1: Interrupt a Swing Worker Thread: Interpret how to use the interrupt () method to interrupt the Worker thread. Example 2: Feedback from Swing Worker thread to the user: an example of an enhancement, add a mode dialog to prompt user input.
Overview: SwingWorker class
Because the SwingWorker class is not part of the Java release, you need to download and compile it to use. Its source code is here: SwingWorker.java
SwingWorker profile
The SwingWorker class can be easily and conveniently used to calculate a value in a new thread. To use this class, you only have a SwingWorker subclass and overwrite the SwingWorker.Construct () method to perform the calculation. Then you instant it and call the start () method on this new instance. For example, a thread is generated in the following code snippet, which constructs a string. Then, the get () method is used to obtain the value returned by the construct method before, and will wait in the case. Finally, the value of the string is displayed on the display. Swingworker worker = new swingworker () {transn "Hello" "" "world";}}; worker.start (); system.out.println ("forker.get (). Tostring () ); In actual applications, constructRuct methods will do more useful (but may take time). For example, it may do one of the following work:
Perform a lot of calculation execution may result in a large number of loaded code to block the network or disk I / O to block waiting for other resources
The characteristic of a SwingWorker class that is not displayed in the above code snippet is that SwingWorker allows you to perform some code in the event delivery line after the construct () returns. You can do this by overlying the Finished () method of SwingWorker. Typically, you can use Finished () to display some components that have just been constructed or set data displayed on the component. One limitation of the original version of the SwingWorker class is that once a Worker thread starts running, you can't interrupt it. (Translation: This article, the examples of this article and the SwingWorker class have been updated. No matter how to say, for a interactive application, let users wait until the work thread is complete. If the user wants to terminate an operation being executed, the thread that performs this should be aborted as soon as possible.
Using an Interrupt () method
An interrupt () method is added to the second edition SwingWorker class to allow interrupt a thread. Your thread should be interrupted by one of two ways: a thread, such as a Sleep () or wait () method is being execrupting an InterruptedException when Interrupt () is called. The thread can be explicitly asked if it has been interrupted, by the following code: thread.interrupted ()
Using the working thread of the SLEEP () or wait () method (such as threads in the following example) generally does not require explicit check whether it is interrupted. It is usually made to throw an instalRuptedException in the SLEEP () or wait () method. However, if you want to interrupt SwingWorker that does not contain timed cycles, you still need to explicitly check the interrupt with an interrupted () method.
Example of introducing a Worker thread
The remainder of this article discusses a program that contains two Worker threads. The picture below is a screenshot of the master window, and a dialog that it pops up: The source code of the example consists of the following files:
SwingWorker.java threadsexample.java example1.java eXample2.java
You can download a ZIP file that contains all the files above. In this zip file, there is also an example of an explanatory in the HTML format, and the example of this version can explain the problem (but more complicated). Before running this example: /usr/local/java/jdk1.3.0/bin/javac threadsexample.java Run this example with the following command line: /usr/local/java/jdk1.3.0/bin/java threadsexample when you Press the "START" button and the corresponding example of the Worker thread will be created. You can view its progress in your schedule. You can press the "CANCEL" button to interrupt the Worker thread. At the startup example, you will see a dialog prompt you to confirm if it is executed. Let us later in detail later.
Example 1: Interrupt Swing Worker thread
The example we want to discuss is Example1.java. The Worker thread in this example contains a loop that performs 100 times and sleeps half a second between the two cycles. // ProgressBar Maximum Is Numloops (100) ProgressBar's maximum value is Numloops (100) for (int i = 0; i