• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java: Display Time  RSS feed

 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Here I am trying to view the time. It is getting displayed but not continuing with the real time. For an example, if I execute the program at 12.00am then the time is just "12.00.00" but when it is 12..00.01, it is not displaying. what is the reason? Here is my code





Please help...
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every second, you need to update the time using the JLabel#setText(). One way of doing it is using the Timer class
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please give me a coding help?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the API docs for the javax.swing.Timer for a code example
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the help!
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yohan, please CarefullyChooseOneForum. Your other thread in the Threads and Synchronization section has been locked. You can continue the discussion here.
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Yohan, please CarefullyChooseOneForum. Your other thread in the Threads and Synchronization section has been locked. You can continue the discussion here.


Hi, but I have marked this thread as solved. My new question is related to threads and that's why I moved to there.
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had to use the timer class. OK, so in here, I am putting that timer inside a thread and that thread isn't working. Here is the new code

Please help
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. You don't need to kick off a new Thread.
2. All updates to Swing components -- that includes setText(..) of a JLabel -- should always be done on the EDT. Go through this tutorial.
3. A Thread can be start()ed only once. Not repeatedly.

Compute and set the label's text from within the Timer's ActionListener's actionPerformed(...).

edit Additionally -

4. Wherever a new Thread is required (which is not here), always prefer implementing Runnable over extending Thread. See the documentation for Runnable to know why.
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need another thread. Swing Timers run on the Event Dispatcher Thread (EDT) of AWT/Swing, and will fire events (actionPerformed) on it. So move the current contents of the run() method to the actionPerformed method, and get rid of the "extends Thread" part and the run() method.

One important lessen for the future though: you can't start a Java thread more than once. You can't restart a thread that has ended. If you need the same work done more than once, use java.lang.Runnable instead and create a new Thread each time.

And another important lessen: concurrency in Swing.
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow..Which means the timer is automatically inside the Event Dispatcher Thread right? So, I don't have to use another thread because we already have a thread there. Ohhh god, that's why we use "timer.start()" isn't it? It is calling to the Event Dispatcher Thread. Am I right?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you're not right. Have you read the Timer API and the tutorial to which I linked in my earlier post? Once you read and digest those, you will be right
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:No, you're not right. Have you read the Timer API and the tutorial to which I linked in my earlier post? Once you read and digest those, you will be right


Yes, I did
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!