Documentation

The Java™ Tutorials
Hide TOC
Bound Properties and Status Methods绑定属性和状态方法
Trail: Creating a GUI With Swing
Lesson: Concurrency in Swing
Section: Worker Threads and SwingWorker

Bound Properties and Status Methods绑定属性和状态方法

SwingWorker supports bound properties, which are useful for communicating with other threads. SwingWorker支持绑定属性,这对于与其他线程通信很有用。Two bound properties are predefined: progress and state. 预定义了两个绑定属性:progressstateAs with all bound properties, progress and state can be used to trigger event-handling tasks on the event dispatch thread.与所有绑定属性一样,progressstate可用于触发事件分派线程上的事件处理任务。

By implementing a property change listener, a program can track changes to progress, state, and other bound properties. 通过实现属性更改侦听器,程序可以跟踪对progressstate和其他绑定属性的更改。For more information, refer to How to Write a Property Change Listener in Writing Event Listeners.有关详细信息,请参阅编写事件侦听器中的如何编写属性更改侦听器

The progress Bound Variableprogress绑定变量

The progress bound variable is an int value that can range from 0 to 100. progress绑定变量是一个范围从0到100的int值。It has a predefined setter method (the protected SwingWorker.setProgress) and a predefined getter method (the public SwingWorker.getProgress).它具有预定义的setter方法(受保护的SwingWorker.setProgress)和预定义的getter方法(公共SwingWorker.getProgress)。

The ProgressBarDemo example uses progress to update a ProgressBar control from a background task. ProgressBarDemo示例使用progress从后台任务更新ProgressBar控件。For a detailed discussion of this example, refer to How to Use Progress Bars in Using Swing Components.有关此示例的详细讨论,请参阅使用Swing组件中的如何使用进度条

The state Bound Variablestate绑定变量

The state bound variable indicates where the SwingWorker object is in its life cycle. state绑定变量指示SwingWorker对象在其生命周期中的位置。The bound variable contains an enumeration value of type SwingWorker.StateValue. 绑定变量包含SwingWorker.StateValue类型的枚举值。Possible values are:可能的值为:

PENDING
The state during the period from the construction of the object until just before doInBackground is invoked.从对象构造到调用doInBackground之前这段时间内的状态。
STARTED
The state during the period from shortly before doInBackground is invoked until shortly before done is invoked.从调用doInBackground之前不久到调用done之前不久的状态。
DONE
The state for the remainder of the existence of the object.对象存在的剩余部分的状态。

The current value of the state bound variable is returned by SwingWorker.getState.SwingWorker.getState返回state绑定变量的当前值。

Status Methods状态方法

Two methods, part of the Future interface, also report on the status of the background task. Future接口的两个方法也报告后台任务的状态。As we saw in Canceling Background Tasks, isCancelled returns true if the task has been canceled. 正如我们在撤消后台任务中看到的,如果任务被取消,isCancelled将返回trueIn addition, isDone returns true if the task has finished, either normally, or by being cancelled.此外,如果任务正常完成或被取消,isDone将返回true


Previous page: Canceling Background Tasks
Next page: Questions and Exercises: Concurrency in Swing