Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-01-2007, 06:30 AM
Member
 
Join Date: Jul 2007
Posts: 40
barney is on a distinguished road
Help with timer in java
Hi, I just started playing with timers today, so I'm not too sure what I'm doing. All I want is to get something simple to work... Here I'm trying to display "test" once every second. Why isn't it working? The timer appears to be running, but the actionevent isn't working.
Code:
import javax.swing.Timer; import java.awt.event.*; public class TimerTest { public static void main( String args[] ) { int delay = 1000; //milliseconds ActionListener taskPerformer = new ActionListener() { public void actionPerformed(ActionEvent evt) { System.out.println( "test" ); } }; Timer test = new Timer(delay, taskPerformer); test.start(); System.out.println( test.isRunning() ); System.out.println( test.getDelay() ); test.stop(); System.out.println( test.isRunning() ); } }
Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-01-2007, 11:24 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
Code:
import javax.swing.Timer; import java.awt.event.*; public class TimerTestRx { public static void main( String args[] ) { int delay = 1000; //milliseconds ActionListener taskPerformer = new ActionListener() { int count = 0; public void actionPerformed(ActionEvent evt) { System.out.println( "count = " + count++ ); if(count > 10) { ((Timer)evt.getSource()).stop(); System.exit(0); } } }; Timer test = new Timer(delay, taskPerformer); test.start(); System.out.println( test.isRunning() ); System.out.println( test.getDelay() ); // Can't do much after we get to this statement. // test.stop(); System.out.println( test.isRunning() ); String s = "<html>We need something to keep the jvm from<br>" + "exiting while we look for some timer activity"; javax.swing.JOptionPane.showMessageDialog(null, s); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to cancel an individual timer in spite of canceling whole timer Java Tip java.util 0 04-04-2008 03:46 PM
Timer usage in java program sandeeprao.techno New To Java 5 01-24-2008 08:16 PM
Simple timer im-not-alive New To Java 1 01-20-2008 10:04 PM
making a count down timer using java saytri New To Java 3 12-29-2007 10:49 PM
Problem with timer in java paul Advanced Java 3 07-26-2007 11:18 AM


All times are GMT +3. The time now is 12:22 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org