Using Calendar class to get time and displaying it on a label using setText.myLabelName command
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   h_kumar
Posted On:   Saturday, March 27, 2004 06:11 AM

I have an application where I am trying to change the text of my label dynamically with the time obtained from the Calendar class. The application seems to be working as the system.out.println statements are printing ok, but the time is not getting displayed on the label dynamically. it gets displayed at times, but it is not very consistent and the application behaves differently on different computers

Re: Using Calendar class to get time and displaying it on a label using setText.myLabelName command

Posted By:   Craig_Wood  
Posted On:   Saturday, March 27, 2004 11:06 AM


import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;

public class ShowingTime
{
public static void main(String[] args)
{
final DateFormat df = new SimpleDateFormat("hh:mm:ss a z");
Calendar calendar = Calendar.getInstance();
df.setCalendar(calendar);
Date now = new Date();
final JLabel timeLabel = new JLabel("time: " +
df.format(now), JLabel.CENTER);
new Timer(1000, new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
timeLabel.setText("time: " + df.format(new Date()));
timeLabel.revalidate();
timeLabel.repaint();
}
}).start();
JPanel panel = new JPanel();
panel.add(timeLabel);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(panel);
f.setSize(400,200);
f.setLocation(200,200);
f.setVisible(true);
}
}
About | Sitemap | Contact