Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


 

Java Tutorials


 

 

Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Java: Example - Text Clock

This is a simple text clock.

See javax.swing.Timer for an explanation of how use this simple timer class.

In this example following swing components are used:

  • JFrame
  • JTextField
  • Timer

You can download, compile, run and test the application on your computer.

The latest version of JDK is needed to run this example code.

 

We have given the complete code of the example.

 

Following coding listing shows the complete code:

 

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
// TextClock.java -- Uses Timer, Calendar, JTextField.
//  -- Fred Swartz, 1999-05-01, 2001-11-02
//  Enhancements: center, leading zeros, uneditable, 12 hour, alarm, timer.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.Calendar;      // only need this one class

////////////////////////////////////////////////////////////////// TextClock
public class TextClock {
    //================================================================= main
    public static void main(String[] args) {
        JFrame clock = new TextClockWindow();
        clock.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        clock.setVisible(true);
    }//end main
}//endclass TextClock


//////////////////////////////////////////////////////////// TextClockWindow
class TextClockWindow extends JFrame {
    //=================================================== instance variables
    private JTextField timeField;  // set by timer listener

    //========================================================== constructor
    public TextClockWindow() {
        // Build the GUI - only one panel
        timeField = new JTextField(6);
        timeField.setFont(new Font("sansserif", Font.PLAIN, 48));

        Container content = this.getContentPane();
        content.setLayout(new FlowLayout());
        content.add(timeField); 
        
        this.setTitle("Text Clock");
        this.pack();

        // Create a 1-second timer and action listener for it.
        // Specify package because there are two Timer classes
        javax.swing.Timer t = new javax.swing.Timer(1000,
              new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      Calendar now = Calendar.getInstance();
                      int h = now.get(Calendar.HOUR_OF_DAY);
                      int m = now.get(Calendar.MINUTE);
                      int s = now.get(Calendar.SECOND);
                      timeField.setText("" + h + ":" + m + ":" + s);
                  }
              });
        t.start();  // Start the timer
    }//end constructor
}//endclass TextClock
Ask programming questions?

 

 

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (post your own) View All Comments Latest 10 Comments:

i thanks to you very much...
i appreciate you...


please send me the easy java codes at my mailing address

Posted by h.m.tahir on Saturday, 10.24.09 @ 10:30am | #92080

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name

 

 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.

[an error occurred while processing this directive]