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 3 of 4 FirstFirst 1234 LastLast
Results 51 to 75 of 81

Thread: Need some help in a timer

  1. #51
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,610
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Need some help in a timer

    I can't tell without seeing it.
    Post the code and the full text of the error message.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Kerooker (April 27th, 2014)

  3. #52
    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) throws InterruptedException {
    		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();
    	  JLabel label = new JLabel("Image and Text",
    								JLabel.CENTER);
     
    	JFrame frame = new JFrame("Teste");
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.getContentPane().add(label);
    	label.setText(teste); 
    	frame.pack();
    	frame.setVisible(true);
    	}
     
    }
    Line 36 - teste cannot be resolved to a variable.

  4. #53
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,610
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Need some help in a timer

    The variable: teste is out of scope where the error happens. It's defined inside a method and is not defined where the code tries to use it. Move the setText statement into the method where teste is defined.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Kerooker (April 27th, 2014)

  6. #54
    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 can't, in that way label won't be resolved to a variable. (it's defined in other method...)

    @@Edit

    I tried putting all the JPane inside the main method, but that won't work either (no error code, just not working.)
    Last edited by Kerooker; April 27th, 2014 at 05:38 PM.

  7. #55
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,610
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Need some help in a timer

    Post the code and the full text of the error messages.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Kerooker (April 27th, 2014)

  9. #56
    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) throws InterruptedException {
    		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();
    	  JLabel label = new JLabel("Text", JLabel.CENTER);
    	  label.setText(teste); 
     
    	JFrame frame = new JFrame("Teste");
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.getContentPane().add(label);
    	frame.pack();
    	frame.setVisible(true);
    	}
     
    }

    label.setText(teste); - Teste cannot be resolved to a variable

    I didn't find a way to call the text in another method...

  10. #57
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,610
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Need some help in a timer

    label.setText(teste); - Teste cannot be resolved to a variable
    The spelling is wrong. The variable is named: teste, not Teste
    If you don't understand my answer, don't ignore it, ask a question.

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

    Kerooker (April 27th, 2014)

  12. #58
    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

    No, the two calls are with the same name. The variable is named 'teste', and I tried to call 'teste', just copied the wrong name from the error, sorry.

  13. #59
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,610
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Need some help in a timer

    The setText() call must be in the same scope with where teste is defined.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Kerooker (April 27th, 2014)

  15. #60
    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 doing it:
    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) throws InterruptedException {
    		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);
    	label.setText(teste); 
     
    	      }
    	  };
    	  new Timer(delay, taskPerformer).start();
    	  JLabel label = new JLabel("Text", JLabel.CENTER);
     
     
    	JFrame frame = new JFrame("Teste");
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.getContentPane().add(label);
    	frame.pack();
    	frame.setVisible(true);
    	}
     
    }
    But then, "label cannot be resolved".

  16. #61
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,610
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Need some help in a timer

    "label cannot be resolved".
    Post the full text showing the statement where the error happened and line number.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Kerooker (April 27th, 2014)

  18. #62
    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

    label cannot be resolved - line 24

  19. #63
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,610
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Need some help in a timer

    What statement is on line 24?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Kerooker (April 27th, 2014)

  21. #64
    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

    label.setText(teste);

  22. #65
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,610
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Need some help in a timer

    label needs to be defined BEFORE it is used. Move its definition ahead of where it is used.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Kerooker (April 27th, 2014)

  24. #66
    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's not possible, or else I would need to cal the label after the main method ended, and it's not possible to declare the Jframe before the timer...

    I need a way to use a variable from the main mathod in the JFrame method.

  25. #67
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,610
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Need some help in a timer

    it's not possible to declare the Jframe before the timer.
    Please explain and post the error messages
    If you don't understand my answer, don't ignore it, ask a question.

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

    Kerooker (April 27th, 2014)

  27. #68
    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 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) throws InterruptedException {
    		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);
    	JLabel label = new JLabel (teste, JLabel.CENTER);
     
    	JFrame frame = new JFrame("Teste");
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.getContentPane().add(label);
    	frame.pack();
    	frame.setVisible(true);
    	      }
    	  };
    	  new Timer(delay, taskPerformer).start();
     
    	}
     
    }
    But it didn't work, without any errors, it just terminate the program without anything in the console.

    I tried putting the JFrame inside the main method.

  28. #69
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,610
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Need some help in a timer

    it didn't work, without any errors,
    Post the full text of the error messages if you need help with them.

    The actionPerformed() method is called once a second. Do you want a new frame created once a second?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Kerooker (April 27th, 2014)

  30. #70
    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

    No error message, just not working.

    I want the frame to be updated every second with the new information, so it can be counted down second to second.

  31. #71
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,610
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Need some help in a timer

    I want the frame to be updated every second
    Updating an existing frame is different from creating a new frame every second.

    You are now back to the problem discussed in post#24 - Thread sleep
    If you don't understand my answer, don't ignore it, ask a question.

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

    Kerooker (April 27th, 2014)

  33. #72
    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

    But if I leave the frame out of the timer method, I cannot call the values in the main method, as they're in different methods

    Should I put the frame before the rest?

  34. #73
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,610
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Need some help in a timer

    yes
    If you don't understand my answer, don't ignore it, ask a question.

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

    Kerooker (April 27th, 2014)

  36. #74
    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) throws InterruptedException {
    		    JLabel label = new JLabel("Text", JLabel.CENTER);
    			JFrame frame = new JFrame("Teste");
    			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			frame.getContentPane().add(label);
    			frame.pack();
    			frame.setVisible(true);
    		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);
    	label.setText(teste);
     
    	      }
    	  };
    	  new Timer(delay, taskPerformer).start();
     
    	}
     
    }

    Error at label.setText(teste);
    Cannot refer to a non-final variable label inside an inner class defined in a different method

  37. #75
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,610
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Need some help in a timer

    Cannot refer to a non-final variable
    Make it final
    If you don't understand my answer, don't ignore it, ask a question.

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

    Kerooker (April 27th, 2014)

Page 3 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