Applet clock demo : Applet « Swing JFC « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Swing JFC » AppletScreenshots 
Applet clock demo
Applet clock demo

//File: applet.html
/*

<applet code="ClockDemo.class"
  codebase="."
  width=150 height=20>
</applet>

*/
///
/*
 * Copyright (c) 2000 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 2nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book (recommended),
 * visit http://www.davidflanagan.com/javaexamples2.
 */

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Label;
import java.text.DateFormat;
import java.util.Date;

/**
 * This applet displays the time, and updates it every second
 */
public class ClockDemo extends Applet implements Runnable {
  Label time; // A component to display the time in

  DateFormat timeFormat; // This object converts the time to a string

  Thread timer; // The thread that updates the time

  volatile boolean running; // A flag used to stop the thread

  /**
   * The init method is called when the browser first starts the applet. It
   * sets up the Label component and obtains a DateFormat object
   */
  public void init() {
    time = new Label();
    time.setFont(new Font("helvetica", Font.BOLD, 12));
    time.setAlignment(Label.CENTER);
    setLayout(new BorderLayout());
    add(time, BorderLayout.CENTER);
    timeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM);
  }

  /**
   * This browser calls this method to tell the applet to start running. Here,
   * we create and start a thread that will update the time each second. Note
   * that we take care never to have more than one thread
   */
  public void start() {
    running = true// Set the flag
    if (timer == null) { // If we don't already have a thread
      timer = new Thread(this)// Then create one
      timer.start()// And start it running
    }
  }

  /**
   * This method implements Runnable. It is the body of the thread. Once a
   * second, it updates the text of the Label to display the current time
   */
  public void run() {
    while (running) { // Loop until we're stopped
      // Get current time, convert to a String, and display in the Label
      time.setText(timeFormat.format(new Date()));
      // Now wait 1000 milliseconds
      try {
        Thread.sleep(1000);
      catch (InterruptedException e) {
      }
    }
    // If the thread exits, set it to null so we can create a new one
    // if start() is called again.
    timer = null;
  }

  /**
   * The browser calls this method to tell the applet that it is not visible
   * and should not run. It sets a flag that tells the run() method to exit
   */
  public void stop() {
    running = false;
  }

  /**
   * Returns information about the applet for display by the applet viewer
   */
  public String getAppletInfo() {
    return "Clock applet Copyright (c) 2000 by David Flanagan";
  }
}

           
       
Related examples in the same category
1. Icon Demo Applet
2. Socket Applet
3. Applet Socket Quote
4. Applet Print
5. Applet System Properties
6. Applet Sound
7. Show Document
8. Applet communication (talk to each other)
9. Get Applets
10. JDBC applet
11. An application and an appletAn application and an applet
12. Applet and Swing ComponentsApplet and Swing Components
13. Icon behavior in JbuttonsIcon behavior in Jbuttons
14. Signed Applet
15. Load resouce in Applet
16. Scribble AppletScribble Applet
17. Toggle buttonToggle button
18. Bouncing CircleBouncing Circle
19. Applet Menu Bar Demo
20. Applet event testerApplet event tester
21. Applet with parameters
22. First applet
23. AppletViewer - a simple Applet Viewer program
24. A simple loan calculator appletA simple loan calculator applet
25. URLButton -- a Button-like object that jumps to a URL
26. Get list of APPLET tags in one HTML file
27. Demonstration of Applet Methods
28. Demonstrates getParameterInfo() and getAppletInfo()
29. Walking Text Demo
30. Java Applets Can Run CGI's (on some browsers)
31. Demo Choice Applet
32. Demo Applet: Connect to Legacy System
33. ShowDocApplet: try out showDocument()
34. Bookmarks
35. Java Wave Applet Demo
36. Slide Puzzle
37. A class to allow use of the ncsa ISMAP format in java applets
38. File Read Applet
39. Applet I18N
40. Image Loader Applet
41. Thread Race Applet
42. Applet: Print from an Applet
43. Your own Applet runtime environment
w__ww_.j_a__va_2_s__.__c__o_m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.