java SwingWorker.doInBackground() 不能访问 GUI 元素 [英] java SwingWorker.doInBackground() must not access GUI elements

查看:31
本文介绍了java SwingWorker.doInBackground() 不能访问 GUI 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是微不足道的,我正在努力理解关于 SwingWorker.

May be this is trivial, I am struggling to understand a simple documentation on SwingWorker.

这里是复制粘贴的内容

工作流程

SwingWorker 的生命周期涉及三个线程:

There are three threads involved in the life cycle of a SwingWorker :

当前线程:在该线程上调用execute()方法.它调度 SwingWorker 在工作线程上执行并返回立即地.可以等待 SwingWorker 使用获取方法.

Current thread: The execute() method is called on this thread. It schedules SwingWorker for the execution on a worker thread and returns immediately. One can wait for the SwingWorker to complete using the get methods.

工作线程:在该线程上调用 doInBackground() 方法.这是所有后台活动都应该发生的地方.通知关于绑定属性更改的 PropertyChangeListeners 使用firePropertyChange 和 getPropertyChangeSupport() 方法.默认情况下有两个绑定属性可用:状态和进度.

Worker thread: The doInBackground() method is called on this thread. This is where all background activities should happen. To notify PropertyChangeListeners about bound properties changes use the firePropertyChange and getPropertyChangeSupport() methods. By default there are two bound properties available: state and progress.

事件调度线程:所有与 Swing 相关的活动都发生在这个线.SwingWorker 调用 process 和 done() 方法和通知此线程上的任何 PropertyChangeListener.

Event Dispatch Thread: All Swing related activities occur on this thread. SwingWorker invokes the process and done() methods and notifies any PropertyChangeListeners on this thread.

通常,当前线程是事件调度线程.

Often, the Current thread is the Event Dispatch Thread.

--

工作线程不是 EDT,因此 doInBackground() 中的代码不能访问 GUI 元素.我的理解正确吗?

The worker thread is not the EDT, hence the code in doInBackground() must not access GUI elements. Is my understanding correct?

背景:我们有一些使用 SwingWorker 的小代码,但有 doInBackground() 创建 FileChooser 并调用 setCurrentDirectory().我怀疑这导致我的异常与 http://bugs.sun.com 几乎相同/bugdatabase/view_bug.do?bug_id=6637181(11-关闭,不是缺陷)

Background: We have small code that uses SwingWorker but has doInBackground() createing FileChooser and calling setCurrentDirectory(). I suspect that is leading me exception almost same as http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6637181 ( 11-Closed, Not a Defect)

推荐答案

是的.从后台线程 - 常规线程和 SwingWorker.doInBackground 不得修改 UI 以避免各种麻烦.

Yes. From a background thread - both regular threads and SwingWorker.doInBackground you must not modify the UI to avoid various trouble.

相反,将更改包装在 Runnable 中,并通过 SwingUtilities.invokeAndWaitSwingUtilities.invokeLater 或 - 在 EDT 中执行它们使用 SwingWorker - 通过 publish(来自 doInBackground).在由 EDT 执行的 SwingWorkerprocess 方法中,您可以访问 GUI.

Instead, wrap the changes in a Runnable and have them executed in the EDT via SwingUtilities.invokeAndWait, SwingUtilities.invokeLater or - when using SwingWorker - via publish (from doInBackground). Within the process method of SwingWorker, which is executed by the EDT, you may access the GUI.

http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html

就个人而言,我发现 invokeLaterinvokeAndWait 在许多情况下更易于使用.SwingWorker 可以用于例如进度条.

Personally, I find invokeLater and invokeAndWait easier to use for many situations. SwingWorker is okay for e.g. progress bars.

这篇关于java SwingWorker.doInBackground() 不能访问 GUI 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆