Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 4 FirstFirst 1234 LastLast
Results 26 to 50 of 81

Thread: Need some help in a timer

  1. #26
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,718
    My Mood
    Mellow
    Thanks
    231
    Thanked 706 Times in 692 Posts

    Default Re: Need some help in a timer

    javax.swing.Timer is not appropriate for what you're doing. I won't explain why here, but if you're interested, read the API.

    The java.util.Timer and an accompanying TimerTask is what you should use. Here's an example of code that creates an instance of a TimerTask and Timer to do what you've described. See if you can write the accompanying TimerTask class. Hint: It looks somewhat like your actionPerformed() method but as a run() method in another class.
            // countingTask is a TimerTask that simply displays a count 1, 2, 3, . . . 
            CountingTask countingTask = new CountingTask();
     
            // create a Timer object
            Timer countTimer = new Timer();
     
            // schedule the countTimer to do the countingTask every second
            // after a 0-second delay
            countTimer.scheduleAtFixedRate( countingTask, 0L, 1000L );

  2. The Following User Says Thank You to GregBrannon For This Useful Post:

    Kerooker (April 27th, 2014)

  3. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,971
    Thanks
    51
    Thanked 2,268 Times in 2,240 Posts

    Default Re: Need some help in a timer

    Post the code you are having problems with.

    The Swing timer may fit OK because the final version of this program will have the timer updating the GUI.
    If you don't understand my answer, don't ignore it, ask a question.

  4. The Following User Says Thank You to Norm For This Useful Post:

    Kerooker (April 27th, 2014)

  5. #28
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default Re: Need some help in a timer

    package cupTimer;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.GregorianCalendar;
    import javax.swing.* ;
     
    public class myTimer {
     
    	public static void main(String[] args) {
    		long tempoEmMs = System.currentTimeMillis() ;
    		System.out.println ("The time is: " + tempoEmMs);
     
    	GregorianCalendar today = new GregorianCalendar();
    	int day =today.get(GregorianCalendar.DAY_OF_MONTH);
    	int month =today.get(GregorianCalendar.MONTH);
    	int year =today.get(GregorianCalendar.YEAR);
    	System.out.println ("The day of today is: " + day + "/" + month + "/" + year);
     
    	GregorianCalendar cup = new GregorianCalendar(2014,5,12);
    	long thisTimeInMs = cup.getTimeInMillis();
    	System.out.println ((thisTimeInMs - tempoEmMs) / 86400000) ;
    	System.out.println ((thisTimeInMs - tempoEmMs) / 2073600000) ;
    	int dayCup = cup.get(GregorianCalendar.DAY_OF_MONTH);
    	int monthCup = cup.get(GregorianCalendar.MONTH);
    	int yearCup = cup.get(GregorianCalendar.YEAR);
    	System.out.println ("Cup day: " + dayCup + "/" + monthCup + "/" + yearCup) ;
     
    	//timer attempt 1 //	
    	int delay = 1000; //milliseconds
    	  ActionListener taskPerformer = new ActionListener() {
    	      public void actionPerformed(ActionEvent evt) {
    	          System.out.println("Teste");
     
    	      }
    	  };
    	  new Timer(delay, taskPerformer).start();
    	}
     
    }

    Is the code i'm having problem with.
    It shows no error, but won't work at all...

  6. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,971
    Thanks
    51
    Thanked 2,268 Times in 2,240 Posts

    Default Re: Need some help in a timer

    but won't work at all..
    Please explain what happens when the code is executed.

    I see you didn't follow my what I suggested in post#24.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    Kerooker (April 27th, 2014)

  8. #30
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default Re: Need some help in a timer

    Anywhere I could put the Thread would bug the code...
    The code executes, and display the info

    The time is: 1398620173865
    The day of today is: 27/3/2014
    45
    1
    Cup day: 12/5/2014


    But it won't execute the last step, posting 'Test1' every second

  9. #31
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,971
    Thanks
    51
    Thanked 2,268 Times in 2,240 Posts

    Default Re: Need some help in a timer

    Did you read post#24?
    If you don't understand my answer, don't ignore it, ask a question.

  10. The Following User Says Thank You to Norm For This Useful Post:

    Kerooker (April 27th, 2014)

  11. #32
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default Re: Need some help in a timer

    Yes;
    I just couldn't find a way to do it. Having that in mind, I decided to try a simpler script for printing a string every second, which didn't work...
    And trying the Thread.sleep is impossible at any line of the code..

  12. #33
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,971
    Thanks
    51
    Thanked 2,268 Times in 2,240 Posts

    Default Re: Need some help in a timer

    trying the Thread.sleep is impossible at any line of the code.
    What does that mean?
    If you get error messages, copy the full text and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  13. The Following User Says Thank You to Norm For This Useful Post:

    Kerooker (April 27th, 2014)

  14. #34
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default Re: Need some help in a timer

    Trying to put in one of the last lines:
    package cupTimer;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.GregorianCalendar;
    import javax.swing.* ;
     
    public class myTimer {
     
    	public static void main(String[] args) {
    		long tempoEmMs = System.currentTimeMillis() ;
    		System.out.println ("The time is: " + tempoEmMs);
     
    	GregorianCalendar today = new GregorianCalendar();
    	int day =today.get(GregorianCalendar.DAY_OF_MONTH);
    	int month =today.get(GregorianCalendar.MONTH);
    	int year =today.get(GregorianCalendar.YEAR);
    	System.out.println ("The day of today is: " + day + "/" + month + "/" + year);
     
    	GregorianCalendar cup = new GregorianCalendar(2014,5,12);
    	long thisTimeInMs = cup.getTimeInMillis();
    	System.out.println ((thisTimeInMs - tempoEmMs) / 86400000) ;
    	System.out.println ((thisTimeInMs - tempoEmMs) / 2073600000) ;
    	int dayCup = cup.get(GregorianCalendar.DAY_OF_MONTH);
    	int monthCup = cup.get(GregorianCalendar.MONTH);
    	int yearCup = cup.get(GregorianCalendar.YEAR);
    	System.out.println ("Cup day: " + dayCup + "/" + monthCup + "/" + yearCup) ;
     
    	//timer attempt 1 //	
    	int delay = 1000; //milliseconds
    	  ActionListener taskPerformer = new ActionListener() {
    	      public void actionPerformed(ActionEvent evt) {
    	          System.out.println("Teste");
     
    	      }
    	  };
    	  new Timer(delay, taskPerformer).start();
    	} Thread.sleep(100000);
     
    }
    Gets me Syntax error, missplaced constructor
    and Syntax error on token 100000, delete this token

  15. #35
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,971
    Thanks
    51
    Thanked 2,268 Times in 2,240 Posts

    Default Re: Need some help in a timer

    That statement is outside of the main() method. It should be moved inside of the {}s for the main() method.
    If you don't understand my answer, don't ignore it, ask a question.

  16. The Following User Says Thank You to Norm For This Useful Post:

    Kerooker (April 27th, 2014)

  17. #36
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default Re: Need some help in a timer

    I also tried that, placing right before the ending }
    It gives me: Unhandled exception type InterruptedException

  18. #37
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,971
    Thanks
    51
    Thanked 2,268 Times in 2,240 Posts

    Default Re: Need some help in a timer

    Unhandled exception type InterruptedException
    That means the statement can throw an exception and the compiler requires that it be inside of a try{} catch block to handle the exception. Or add "throws Exception" to the main() method's definition.

    --- Update ---

    The String: teste was given a value and it is never changed after that. It will always have the same content.
    If you don't understand my answer, don't ignore it, ask a question.

  19. The Following User Says Thank You to Norm For This Useful Post:

    Kerooker (April 27th, 2014)

  20. #38
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default Re: Need some help in a timer

    I made a smaller code, with only the important information:
    package cupTimer;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.GregorianCalendar;
     
    import javax.swing.* ;
     
    public class myTimer {
     
    	public static void main(String[] args) {
    		long tempoEmMs = System.currentTimeMillis() ; 
    	GregorianCalendar today = new GregorianCalendar(); 
    	GregorianCalendar cup = new GregorianCalendar(2014,5,12,0,0);
    	long thisTimeInMs = cup.getTimeInMillis();
    	long daysLeft = ((thisTimeInMs - tempoEmMs) / 86400000);
    	int hoursLeft = (24 - today.get(GregorianCalendar.HOUR_OF_DAY));
    	int minutesLeft = (60 - today.get(GregorianCalendar.MINUTE));
    	int secondsLeft = (60 - today.get(GregorianCalendar.SECOND));
    	final String teste = ("Missing:" + daysLeft + " Days " + hoursLeft + " Hours " + minutesLeft + " Minutes " + secondsLeft + " Seconds");
     
    	int delay = 1000; //milliseconds
    	  ActionListener taskPerformer = new ActionListener() {
    	      public void actionPerformed(ActionEvent evt) {
    	          System.out.println(teste);
     
    	      }
    	  };
    	  new Timer(delay, taskPerformer).start();
    	  try {
    		Thread.sleep(100000);
    	} catch (InterruptedException e) {
    		e.printStackTrace();
    }
    	}
     
    }

    But I can't get it to update the time, it'll only get the information on the final string. How can I use the string without putting the "Final" argument? I need it to use that string, but using the updated and new version of it everytime.

    --- Update ---

    Replying to the:
    "--- Update ---

    The String: teste was given a value and it is never changed after that. It will always have the same content."

    How can I use the string again with different content?

  21. #39
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,971
    Thanks
    51
    Thanked 2,268 Times in 2,240 Posts

    Default Re: Need some help in a timer

    How can I use the string again with different content?
    Assign it a new value using an assignment statement.
    Move the computations inside of the actionPerformed method.
    If you don't understand my answer, don't ignore it, ask a question.

  22. The Following User Says Thank You to Norm For This Useful Post:

    Kerooker (April 27th, 2014)

  23. #40
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default Re: Need some help in a timer

    I made it work with this:
    package cupTimer;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.GregorianCalendar;
     
    import javax.swing.* ;
     
    public class myTimer {
     
    	public static void main(String[] args) {
    		int delay = 1000; //milliseconds
    		  ActionListener taskPerformer = new ActionListener() {
    		      public void actionPerformed(ActionEvent evt) {
    	long tempoEmMs = System.currentTimeMillis() ; 
    	GregorianCalendar today = new GregorianCalendar(); 
    	GregorianCalendar cup = new GregorianCalendar(2014,5,12,0,0);
    	long thisTimeInMs = cup.getTimeInMillis();
    	long daysLeft = ((thisTimeInMs - tempoEmMs) / 86400000);
    	int hoursLeft = (24 - today.get(GregorianCalendar.HOUR_OF_DAY));
    	int minutesLeft = (60 - today.get(GregorianCalendar.MINUTE));
    	int secondsLeft = (60 - today.get(GregorianCalendar.SECOND));
    	String teste = ("Missing:" + daysLeft + " Days " + hoursLeft + " Hours " + minutesLeft + " Minutes " + secondsLeft + " Seconds");
     
     
    	          System.out.println(teste);
     
    	      }
    	  };
    	  new Timer(delay, taskPerformer).start();
    	  try {
    		Thread.sleep(100000);
    	} catch (InterruptedException e) {
    		e.printStackTrace();
    }
    	}
     
    }



    I believe that after the timer describerd in "Thread" ends, the timer will stop. Is that right? How can I make it go forever?

  24. #41
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,971
    Thanks
    51
    Thanked 2,268 Times in 2,240 Posts

    Default Re: Need some help in a timer

    I thought that the final project was to have a GUI that displayed the count down. When the code is changed to use a GUI there won't be a problem requiring the Thread.sleep.
    If you don't understand my answer, don't ignore it, ask a question.

  25. The Following User Says Thank You to Norm For This Useful Post:

    Kerooker (April 27th, 2014)

  26. #42
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default Re: Need some help in a timer

    Ah, alright.
    I don't have any idea on how to start a GUI, have only been experimenting in Eclipse's IDE, never ran something expecting it to work... Would you send me a tutorial, or even some tips?

  27. #43
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,971
    Thanks
    51
    Thanked 2,268 Times in 2,240 Posts

    Default Re: Need some help in a timer

    The Swing Tutorial: Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

    Basically:
    create a JFrame
    create a JLabel and add it to the JFrame
    Have the actionPerformed method update the text in the JLabel.
    If you don't understand my answer, don't ignore it, ask a question.

  28. The Following User Says Thank You to Norm For This Useful Post:

    Kerooker (April 27th, 2014)

  29. #44
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default Re: Need some help in a timer

    Should I create another file for it? Or isn't it possible?

  30. #45
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,971
    Thanks
    51
    Thanked 2,268 Times in 2,240 Posts

    Default Re: Need some help in a timer

    The few lines of code can be added to what you have.
    If you don't understand my answer, don't ignore it, ask a question.

  31. The Following User Says Thank You to Norm For This Useful Post:

    Kerooker (April 27th, 2014)

  32. #46
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default Re: Need some help in a timer

    package cupTimer;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.GregorianCalendar;
    import javax.swing.* ;
     
    public class myTimer {
     
    	public static void main(String[] args) {
    		int delay = 1000; //milliseconds
    		  ActionListener taskPerformer = new ActionListener() {
    		      public void actionPerformed(ActionEvent evt) {
    	long tempoEmMs = System.currentTimeMillis() ; 
    	GregorianCalendar today = new GregorianCalendar(); 
    	GregorianCalendar cup = new GregorianCalendar(2014,5,12,0,0);
    	long thisTimeInMs = cup.getTimeInMillis();
    	long daysLeft = ((thisTimeInMs - tempoEmMs) / 86400000);
    	int hoursLeft = (24 - today.get(GregorianCalendar.HOUR_OF_DAY));
    	int minutesLeft = (60 - today.get(GregorianCalendar.MINUTE));
    	int secondsLeft = (60 - today.get(GregorianCalendar.SECOND));
    	String teste = ("Missing:" + daysLeft + " Days " + hoursLeft + " Hours " + minutesLeft + " Minutes " + secondsLeft + " Seconds");
     
     
    	          System.out.println(teste);
     
    	      }
    	  };
    	  new Timer(delay, taskPerformer).start();
    	  try {
    		Thread.sleep(100000);
    	} catch (InterruptedException e) {
    		e.printStackTrace();
    }
     
    	JLabel label = new JLabel("Image and Text",
    								JLabel.CENTER);
     
    	JFrame frame = new JFrame("Teste");
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.getContentPane().add(label);
    	frame.pack();
    	frame.setVisible(true);
    	}
     
    }

    I created the JPane (in the and) and am not sure on what to do with it..

  33. #47
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,971
    Thanks
    51
    Thanked 2,268 Times in 2,240 Posts

    Default Re: Need some help in a timer

    what to do with it..
    Call its setText method from the actionPerformed() method to show the countdown times.

    Remove the Thread sleep statement. The open GUI window will keep the program from exiting now.
    If you don't understand my answer, don't ignore it, ask a question.

  34. The Following User Says Thank You to Norm For This Useful Post:

    Kerooker (April 27th, 2014)

  35. #48
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default Re: Need some help in a timer

    I tried
    label.setText(teste);

    But it won't work, I also tried label.setText (taskPerformed.getText);
    But I 'knew' it wouldn't work... What's the value I need to get?

  36. #49
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,971
    Thanks
    51
    Thanked 2,268 Times in 2,240 Posts

    Default Re: Need some help in a timer

    it wouldn't work.
    If you get error messages, copy the full text and paste it here.

    Otherwise what does "it wouldn't work" mean?
    If you don't understand my answer, don't ignore it, ask a question.

  37. The Following User Says Thank You to Norm For This Useful Post:

    Kerooker (April 27th, 2014)

  38. #50
    Member
    Join Date
    Apr 2014
    Posts
    45
    My Mood
    Cool
    Thanks
    35
    Thanked 0 Times in 0 Posts

    Default Re: Need some help in a timer

    It won't get the value, it says that "teste" cannot be resolved to a variable

Page 2 of 4 FirstFirst 1234 LastLast

Similar Threads

  1. Swing TIMER
    By newtolearningjava in forum What's Wrong With My Code?
    Replies: 14
    Last Post: April 13th, 2014, 04:01 PM
  2. [SOLVED] both class javax.swing.Timer in javax.swing and class java.util.Timer in java.util match
    By stresstedout in forum What's Wrong With My Code?
    Replies: 13
    Last Post: April 10th, 2014, 07:32 PM
  3. Countdown timer
    By AANZ in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 1st, 2013, 07:46 AM
  4. Ticked Off Timer
    By JavaCow in forum Loops & Control Statements
    Replies: 8
    Last Post: October 30th, 2010, 09:18 AM
  5. Timer?
    By TimW in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 27th, 2009, 07:43 AM