Click here to Skip to main content
Licence CPOL
First Posted 12 Mar 2003
Views 81,215
Downloads 4,036
Bookmarked 21 times

Digital Clock

By | 12 Mar 2003 | Article
A digital clock JavaBean for use in a JavaBean aware IDE
Sample Image - rldclock.gif

Introduction

Many applications provide a status bar at the bottom of the application. This is used to provide the user with some information. The type and amount of information varies with the application. A common piece of information that you might see is a digital clock. I wanted the ability to quickly and easily add a digital clock to my application status bar while in my IDE.

Background

I use the NetBeans IDE for my development. It makes the development of an application's Graphical User Interface (GUI) much easier than just using a text editor. Using the IDE, you can do Java GUI development much like you do in a Visual Basic application. You place the components onto the form and then modify the properties. This allows you to see what the form will look like at design time. When using a text editor to create your code, you have to compile the application and then execute it to see what the final layout will be. This can be tedious and time consuming.

Similar to the component tab in the Visual Basic IDE, the NetBeans IDE also has a component tab. These components are written as JavaBeans. The IDE gives you the ability to add your own JavaBean components to the IDE Component tabs.

Using the Code

To fully utilize the advantages of a JavaBean, it really needs to be used in an IDE. However, this is not a requirement to use the component. It can very easily be used in your code without the use of an IDE.

If you are using an IDE for your Java development, then you should add the DigitalClock to your IDE's component tab. If you are using NetBeans, you can do this by mounting the DigitalClock.jar file and then right clicking the DigitalClock class. From the context menu, select "Tools" and then "Add to Component Palette..."

Now that you have the component in your IDE you can just add the component to your applications form. You can compile your program and execute it.

If want to use the DigitalClock component in your code and you don't have an IDE, you can still use it. You can declare an instance of the DigitalClock like this:

// Declare an instance of DigitalClock
private com.rl.beans.DigitalClock digitalClock1;
digitalClock1 = new com.rl.beans.DigitalClock();

Once you have created the component, you can then manipulate the properties like you would any other component.

// Set the format of the clock.
digitalClock1.setFormat(" hh:mm:ss ");
digitalClock1.setUpdateInterval(1000);//Update every 1000ms
digitalClock1.setEditable(false);

Possible Enhancements

The method I use to implement the interval is to update the text and then sleep for the updateInterval. This has the drawback that the next update will occur at the updateInterval + (execution time of update()). This should not be a real problem since the clock is not designed for precision time keeping.

The DigitalClock could be easily modified to use a Timer. I opted not to use the Timer because it suffers from some of the same problems. If you really want a more precise method of updating the clock, you will need to change to a more robust multi-threaded version.

Summary

The use of this JavaBean allows you to easily add a digital clock to your application or applet. The basic framework of the bean can also be used to build more advanced JavaBeans that serve other purposes.

Happy coding.

History

  • 12th March, 2003: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Robert Little

Web Developer

United States United States

Member

He is currently employed at global telecommunications solutions and services provider where he fills the role of a senior developer working on IVR and CTI projects in just about any technical capacity that is required.
 
He has been programming since 1992 and has a degree in Computer Science. His programming experience includes C/C++, Perl, Visual Basic, Java, and many others. With the exception of C, these languages are and continue to be self-taught.
 
Robert can be contacted at ca_rlittle at hotmail dot com.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberMember 45099254:12 23 Sep '09  
Generalpackage com.rl.beans; Pinmembersteveclm6:11 23 Oct '07  
Generalreference to custom debug class needs to be removed Pinmemberzuluzulu5:31 3 Aug '05  
GeneralRe: reference to custom debug class needs to be removed Pinmemberemorejjerome20:06 23 Apr '07  
Generaldownload problem Pinmembersarala hadwale16:38 28 Dec '03  
GeneralRe: download problem PinmemberRobert Little0:07 7 Jan '04  
GeneralRe: download problem PinsussAnonymous0:14 7 Jan '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 13 Mar 2003
Article Copyright 2003 by Robert Little
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid