Results 1 to 11 of 11

Thread: looping time{resolved}

  1. #1

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Resolved looping time{resolved}

    I have a simple program that displays the time, date and stuff like that. The only problem is it just shows one constant time. I have tried several diffrent ways to try to loop it but I can't seem to get it to work. Here is the code I have so far:

    Code:
     
     import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    
    public class ClockPanel2 extends JPanel
    {
     public ClockPanel2()
     {
       super();
       String currentTime = getTime();
       JLabel time = new JLabel("Time: ");
       JLabel current = new JLabel(currentTime);
       add(time);
       add(current);
     }
     
     String getTime()
     {
     	String time;
    	Calendar now = Calendar.getInstance();
    	int hour = now.get(Calendar.HOUR_OF_DAY);
    	int minute = now.get(Calendar.MINUTE);
    	int second = now.get(Calendar.SECOND);
    	int month = now.get(Calendar.MONTH);
    	int day = now.get(Calendar.DAY_OF_MONTH);
    	int year = now.get(Calendar.YEAR);
    	
    	
    	String monthName = " ";
    	switch (month)
    	{
    	case (1):
    		monthName = "January";
    		break;
    	case (2):
    		monthName = "February";
    		break;
    	case (3):
    		monthName = "March";
    		break;
    	case (4):
    		monthName = "April";
    		break;
    	case (5):
    		monthName = "May";
    		break;
    	case (6):
    		monthName = "June";
    		break;
    	case (7):
    		monthName = "July";
    		break;
    	case (8):
    		monthName = "August";
    		break;
    	case (9):
    		monthName = "September";
    		break;
    	case (10):
    		monthName = "October";
    		break;
    	case (11):
    		monthName = "November";
    		break;
    	case (12):
    		monthName = "December";
          }
    	time = monthName + " " + day + ", " + year + " " + hour + ":" + minute + ":" + second;
    	return time;
     }
    }
    Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class ClockFrame2 extends JFrame
    {
     public ClockFrame2()
     {
       super("Clock");
       setSize(225,125);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       Container pane = getContentPane();
       FlowLayout flo = new FlowLayout();
       pane.setLayout(flo);
       ClockPanel2 time  = new ClockPanel2();
       pane.add(time);
       setContentPane(pane);
       setVisible(true);
      }
      
      
      public static void main(String[] args)
      {
       ClockFrame2 sal = new ClockFrame2();
      }
    }
    Anyone have an idea or a diffrent method on how to keep the time looping???
    Last edited by System_Error; Nov 15th, 2004 at 06:05 AM.

  2. #2

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111
    I was thinking there would have to be another method to do this, because in the above example, the only way to keep it going is like infinite recursion or something.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Use a java.util.Timer.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111
    Thanks. I was hoping someone would have an easier method.

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by System_Error
    Thanks. I was hoping someone would have an easier method.
    Nope. Consider that your gift for today... happy birthday.

  6. #6

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111
    Originally posted by mendhak
    Nope. Consider that your gift for today... happy birthday.

    ahhh...How sweet of you. Thank you!

  7. #7
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Had this hooked to a JButton once. Dunno if it helps.
    Code:
    public void actionPerformed(ActionEvent ae){
       Thread timer = new Thread(this);
       timer.start();
    
    public void run(){
        int seconds = 0;
        int minutes = 0;
        int hours = 0;
        for(;;){
         try{
         Thread.sleep(1000);
           seconds = ++seconds;
            if(seconds == 60){
             minutes = ++minutes;
             seconds = 0;
             }
            if(minutes == 60){
             hours = ++hours;
             minutes = 0;
             }
             tt.timerscreen.setText(new String(hours+":"+ minutes+":"+ seconds));
           }catch(InterruptedException e){;}
          }
         }
        }

  8. #8

  9. #9

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111
    Thanks. That helps a bunch!

  10. #10
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Did it really? I wasen't sure if it would. My marijuana consumption has gone up dramatically within the past few weeks.

  11. #11

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111
    Originally posted by Dilenger4
    My marijuana consumption has gone up dramatically within the past few weeks.
    Don't worry. It's only natural.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width