Click here to Skip to main content
15,887,175 members
Articles / Programming Languages / Java / Java SE

Digital Clock

Rate me:
Please Sign up or sign in to vote.
3.10/5 (10 votes)
12 Mar 2003CPOL3 min read 126.9K   6.7K   23   7
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:

Java
// 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.

Java
// 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)


Written By
Software Developer
United States United States
Robert has been programming since 1992 and has a degree in Computer Science. Robert's programming experience includes C/C++, Perl, Visual Basic, Java, C#, ASP.NET 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.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Member 450992523-Sep-09 4:12
Member 450992523-Sep-09 4:12 
Generalpackage com.rl.beans; Pin
steveclm23-Oct-07 6:11
steveclm23-Oct-07 6:11 
Generalreference to custom debug class needs to be removed Pin
zuluzulu3-Aug-05 5:31
zuluzulu3-Aug-05 5:31 
GeneralRe: reference to custom debug class needs to be removed Pin
emorejjerome23-Apr-07 20:06
emorejjerome23-Apr-07 20:06 
Generaldownload problem Pin
sarala hadwale28-Dec-03 16:38
sarala hadwale28-Dec-03 16:38 
GeneralRe: download problem Pin
Robert Little7-Jan-04 0:07
Robert Little7-Jan-04 0:07 
GeneralRe: download problem Pin
Anonymous7-Jan-04 0:14
Anonymous7-Jan-04 0:14 

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

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