• Post Reply Bookmark Topic Watch Topic
  • New Topic

javax.swing.Timer problem  RSS feed

 
Supun Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys!,

I created a timer and start it in main method.
but it isn't working.

I would like to know why?

here is the code i tried.



Note: If I create the timer in constructor. It works as it should.
Thanks!

-Supun
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program ends (JVM exits) soon after timer.start(); so your timer is not given a chance to run.
You can pause the main thread with Thread.sleep(5000); soon after starting the timer to give it a chance to run before main exits.

 
Supun Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E Armitage wrote:The program ends (JVM exits) soon after timer.start(); so your timer is not given a chance to run.
You can pause the main thread with Thread.sleep(5000); soon after starting the timer to give it a chance to run before main exits.


Thanks!

But I was expecting more detailed answer with Thread (Daemon Thread).
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Supun Lakshan Dissanayake wrote:Thanks!

But I was expecting more detailed answer with Thread (Daemon Thread).
Why?

You use a swing timer with GUIs because they execute on the EDT and when you have GUI elements there is at least one alive non-daemon thread while there is at least one displayable AWT or Swing component within the application. Your code doesn't have any GUI components. If you are not doing swing then you should use a util.Timer instead. If you are doing swing and want to receive answers that have relevance to your swing application then you should post code that has swing components rather than force people to guess at what you are doing.
 
Supun Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E Armitage wrote:
Why?

You use a swing timer with GUIs because they execute on the EDT and when you have GUI elements there is at least one alive non-daemon thread while there is at least one displayable AWT or Swing component within the application. Your code doesn't have any GUI components. If you are not doing swing then you should use a util.Timer instead. If you are doing swing and want to receive answers that have relevance to your swing application then you should post code that has swing components rather than force people to guess at what you are doing.


Thanks!
I was just experimenting.
What is the difference between java.util.Timer and javax.swing.Timer?
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.util.Timer was added in Java 1.3 and has more features while being more general purpose than swing.Timer which is designed to make it easy to use in swing GUI applications. Open the API docs for both them and read to find out more about them.
 
Supun Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!