Click here to Skip to main content
Click here to Skip to main content

Digital Clock

, 12 Mar 2003 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Robert Little
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.
Follow on   Google+

Comments and Discussions

 
GeneralMy vote of 1 PinmemberMember 450992523-Sep-09 5:12 
Generalpackage com.rl.beans; Pinmembersteveclm23-Oct-07 7:11 
Generalreference to custom debug class needs to be removed Pinmemberzuluzulu3-Aug-05 6:31 
GeneralRe: reference to custom debug class needs to be removed Pinmemberemorejjerome23-Apr-07 21:06 
Generaldownload problem Pinmembersarala hadwale28-Dec-03 17:38 
GeneralRe: download problem PinmemberRobert Little7-Jan-04 1:07 
GeneralRe: download problem PinsussAnonymous7-Jan-04 1:14 

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.

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