PDA

Click to See Complete Forum and Search --> : looping time{resolved}


System_Error
Nov 12th, 2004, 02:47 PM
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:



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;
}
}



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???

System_Error
Nov 14th, 2004, 12:38 PM
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.

CornedBee
Nov 14th, 2004, 03:18 PM
Use a java.util.Timer.

System_Error
Nov 15th, 2004, 05:05 AM
Thanks. I was hoping someone would have an easier method.

mendhak
Nov 16th, 2004, 12:21 AM
Originally posted by System_Error
Thanks. I was hoping someone would have an easier method.

Nope. Consider that your gift for today... happy birthday. :D

System_Error
Nov 16th, 2004, 05:11 PM
Originally posted by mendhak
Nope. Consider that your gift for today... happy birthday. :D


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

Dillinger4
Nov 17th, 2004, 02:20 AM
Had this hooked to a JButton once. Dunno if it helps.

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){;}
}
}
}

Dillinger4
Nov 17th, 2004, 02:40 AM
Think i should have threw another if in there. Wouldn't want to go over 24 hrs. :lol:

System_Error
Nov 17th, 2004, 04:24 PM
Thanks. That helps a bunch!

Dillinger4
Nov 17th, 2004, 05:32 PM
Did it really? I wasen't sure if it would. My marijuana consumption has gone up dramatically within the past few weeks. :lol:

System_Error
Nov 17th, 2004, 05:57 PM
Originally posted by Dilenger4
My marijuana consumption has gone up dramatically within the past few weeks. :lol:

Don't worry. It's only natural.:bigyello: