• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Add seconds count on a jlabel using thread

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. I want to add seconds count on a jlabel using Thread, while i move the jlabel using keyboard directions keys. How can you help me? I want to use a thread, not a Timer component.    

 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I want to use a thread,  


Can you explain your thoughts on what the Thread would do and why you want to use one?
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

spike mtz wrote:. . . I want to use a thread, not a Timer component. . . .

No you don't. You can get into no end of trouble by threading Swing® components wrongly; they are not thr‍ead‑safe.
 
spike metzer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now, my code is like this


I add a Thread, but app runs very slow and crashing... i want to do something like this, but with no runs delay.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

and crashing...  


Please copy the full text of the error message and paste it here. It has important info about the error.
 
spike metzer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

and crashing...  


Please copy the full text of the error message and paste it here. It has important info about the error.



I'm sorry, I'd not speak English very well. I meant "lag" instead of "crashing". There are no error messages.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The while loop has nothing to slow its execution down.  It will loop many thousands of times in less than a second and be calling the invokeLater method each time.  That will cause congestion in the code.
The loop should not call invokeLater so frequently.  It should be called about one time for each change in the least significant digit it displays.  If the display shows 1/10 of seconds, then call it about every .1 second.
 
spike metzer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:The while loop has nothing to slow its execution down.  It will loop many thousands of times in less than a second and be calling the invokeLater method each time.  That will cause congestion in the code.
The loop should not call invokeLater so frequently.  It should be called about one time for each change in the least significant digit it displays.  If the display shows 1/10 of seconds, then call it about every .1 second.



How can i do this?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

spike metzer wrote:
How can i do this?



The easiest way would be to use some sort of flag. Don't call invokeLater() to add a task, until the previous one has completed first. This way, you don't get a runaway effect, where the EDT has thousands of tasks to do, and other tasks (like reading the keyboard/mouse) has to wait their turn.

Henry
 
spike metzer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

spike metzer wrote:
How can i do this?



The easiest way would be to use some sort of flag. Don't call invokeLater() to add a task, until the previous one has completed first. This way, you don't get a runaway effect, where the EDT has thousands of tasks to do, and other tasks (like reading the keyboard/mouse) has to wait their turn.

Henry




Thanks, but I'm a newbie and I'm learning. i don't know how to do this : (
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Thread sleep method is one way to slow down a loop.  Use the difference between the current time and the time in the future when you want to do something to set how long to sleep.
For example if the time is now 0930 and the task is to be done at 0933, then wait 3 minutes.
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use Thread.sleep(). Its behavior is unreliable. In fact, don't use the Thread class at all. There are high-level components you can use for concurrency. In the case of Swing applications, you will want to use either a Timer or a SwingWorker. For your current scenario, Timer is absolutely perfect. Just create the timer, set the delay, set it to repeat, and start.

If you don't want to use it, you'll have to come up with a really good reason for us, because otherwise our other advise is pointless.

 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed, a SwingWorker would be fine, if OP for some reason doesn't want to use a Swing Timer.

A SwingWorker<Void, Integer> seems appropriate. See the SwingWorker tutorial

In the 'doInBackground' method, call 'Thread.sleep(1_000)'. When waking up, measure the time since the last wake up, and in the 'process' method, update the label with the time difference. That way, it doesn't matter much if there is any delay in the waking up of the sleep method.

Note that that tutorial claims that the 'setText' method is thread safe, so you could update the label directly. However, it points to the specifications of setText in the JTextComponent class, but I see nothing regarding thread safety.

@Stephan
what is unreliable about Thread.sleep?
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:. . . Note that that tutorial claims that the 'setText' method is thread safe, so you could update the label directly. . . .

I know there are some people who use this website who disbelieve anything about Swing methods being threa‍d‑safe.

. . . what is unreliable about Thread.sleep?

It can cause the display to hang because the painting cannot be completed in the time the threa‍d is awake.
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:@Stephan
what is unreliable about Thread.sleep?


Threads are not required to actually sleep for the given amount of time. This problem is solved by higher abstraction classes that schedule tasks at a fixed rate or with a fixed delay, and can repeat tasks and optionally coalesce slow tasks. The only methods of the Thread class I had a good reason to call occasionally were interrupt() and interrupted(). Those two have also been replaced by using higher abstraction types properly, such as Future.cancel() and Future.isCanceled().

And think about it, why would you specifically have to tell a thread to sleep a certain amount of time, based on the amount of time that passed since some moment in the past that you have to record, if all of that is already done for you by code that probably does it better?

Again, javax.swing.Timer is absolutely perfect for this job, and even if that's not what you want for some arbitrary reason, a good second choice is SwingWorker, which can be passed to a ScheduledExecutorService.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The OP said: Thanks, but I'm a newbie and I'm learning.
Would learning how the nuts and bolts of a system works be useful before moving on to the more sophisticated and complicated classes that will come in time?
Many people recommend starting with a text editor and command prompt instead of an full IDE so that they can learn what is going on.  Later they will move to the IDE.

When a person is learning to swim do you take them to the diving board at the deep end and show off with a dive, or do you start at the shallow end, walk in and move to a floating position?
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would argue that in this analogy, Timer is the floating device, and Thread is the diving weight.
 
Catch Ernie! Catch the egg! And catch this tiny ad too:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic