Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Digital Clock

0.00/5 (No votes)
12 Mar 2003 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here