0

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 device is over, then I'd be getting the last result of the transmission thread. What would the problem be?

Manoj
  • 5,542
  • 9
  • 54
  • 80
Mouhammed Soueidane
  • 1,056
  • 2
  • 24
  • 42
  • Can you share different code which illustrates the problem? – Peter Lawrey May 05 '11 at 09:37
  • My guess is you are accessing the device in the GUI thread which prevents the GUI from updating. You need to ensure the GUI thread is not blocking on the device or a lock when you want it to update the GUI. – Peter Lawrey May 05 '11 at 09:38
  • No Peter, I'm actually creating the JWindow object from a mediator class. The mediator class starts the transmission thread when it first loads. This same mediator class has a JButton that starts the transmission of the data by calling a method in the transmission thread. Right before the transmission starts, this same mediator class creates the JWindow that contains a JLabel which updates its value from a function inside the transmission thread. – Mouhammed Soueidane May 05 '11 at 09:56
  • You need to get a stack trace to find out what the application is doing when it should be updating the JWindow, but isn't. – Peter Lawrey May 05 '11 at 09:59
  • What do you mean by "calling a method in the transmission thread"? Just because the method is in your TransmissionThread class or TransmissionRunnable class doesn't mean that it's executed in the transmission thread. Without code, it's hard to help. – JB Nizet May 05 '11 at 11:21
  • 1
    "I cannot share the code since it's against the company policy," What does that have to do with anything? We never ask for your production code. We ask for something that DEMONSTRATES the problem. The whole point of creating a SSCCE is to remove unnecessary code. Your question is simply about Timers. There is noting special about that, everybody uses Timers. – camickr May 05 '11 at 15:22

2 Answers2

2

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

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255