开发者

Update JLabel from another thread

开发者 https://www.devze.com 2023-03-03 16:33 出处:网络
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.

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.

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 de开发者_运维技巧vice is over, then I'd be getting the last result of the transmission thread. What would the problem be?


Instead of java.util.Timer, javax.swing.Timer may be more convenient, as seen in this example.


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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消