从另一个线程更新JLabel [英] Update JLabel from another thread

查看:289
本文介绍了从另一个线程更新JLabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的家伙,
我有摇摆相关的问题。我无法分享代码,因为它违反了公司政策,所以我会尽力解释这个问题。

Dear guys, I'm having swing related problem. I cannot share the code since it's against the company policy, so I will try my best to explain the problem.

简而言之,我创建了一个扩展JWindow的类包含一个JLabel。此JLabel的文本通过计时器对象随机更新,该对象使用scheduleAtFixedRate方法每50 ms实例化一次TimerTask。通过在一个单独的线程中调用一个方法(让我们称之为传输线程)来处理JLabel中的值,该线程处理将数据传输到某个设备。问题是JWindow出现在屏幕上没有任何内容,直到传输到设备结束,然后我将得到传输线程的最后结果。问题是什么?

In short, I have created a class that extends JWindow that contains a JLabel. This JLabel's text is updated randomly through a timer object, that instantiates a TimerTask every 50 ms using the scheduleAtFixedRate method. The values in the JLabel are retrieved by calling a method in a separate thread (let's call it transmission thread) that handles transmitting data to a certain device. The problem is that the JWindow appears on the screen with no content whatsoever till the transmission to the device is over, then I'd be getting the last result of the transmission thread. What would the problem be?

推荐答案

问题是您访问除事件调度线程(EDT)之外的其他线程中的swing组件。这是禁止的。

The problem is that you access swing components in other threads than the Event Dispatch Thread (EDT). This is forbidden.

每个swing组件的javadoc都有一个指向此页面的链接: http://download.oracle.com/javase/6/docs/api/javax/swing/package-summary.html#线程,其中详细解释了线程策略。

The javadoc of every swing componenthas a link to this page : http://download.oracle.com/javase/6/docs/api/javax/swing/package-summary.html#threading, where the threading policy is explained in details.

这篇关于从另一个线程更新JLabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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