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:
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.
digitalClock1.setFormat(" hh:mm:ss ");
digitalClock1.setUpdateInterval(1000);
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