Hello! Trying to Get Java Swing GUI to display clock/timer in jTextField

Hello,

Please help! I'm relatively new to Java and trying to develop an app that include a time-dependent function:
-at a specified time interval, a jTextField in a jFrame will update with the real time, thus displaying a digital clock
-at the time interval, other code may be executed as well.

I've found many clock apps online, and different approaches, and tried all kinds of approaches, but none perform this simple function other than as a standalone app. Spent many many hours over the last MONTH trying to figure this out, but now throw in the towel for the experts.

Re: Hello! Trying to Get Java Swing GUI to display clock/timer in jTextField

JAVA is your best friend when it comes to not having to do the work yourself.

I would advise looking into the GregorianCalendar object (GregorianCalendar (Java Platform SE 6)). Why this? Because this can give you the current time, day, day of week, ect. Really anything you want to know about dates or deal with dates can be done with the GregorianCalendar object.

Here is a sample code to print out the current time(it is actually this easy):

As for updating when the time changes, my first place to look would be if there is some sort of object that will throw an event when the time changes. If there is, then that could be an easy way of listening for a time change. Alternatively, you would want to put on a delay of 1 minute for, well I guess eternity. Doesn't seem like the best solution. I'm sure there is an elegant way of doing it.

January 26th, 2011, 11:21 AM

KevinWorkman

Re: Hello! Trying to Get Java Swing GUI to display clock/timer in jTextField

Quote:

Originally Posted by aussiemcgr

As for updating when the time changes, my first place to look would be if there is some sort of object that will throw an event when the time changes. If there is, then that could be an easy way of listening for a time change. Alternatively, you would want to put on a delay of 1 minute for, well I guess eternity. Doesn't seem like the best solution. I'm sure there is an elegant way of doing it.

Re: Hello! Trying to Get Java Swing GUI to display clock/timer in jTextField

After considerable effort, I FINALLY managed to produce a working jTextField clock! Special thanks to online discussion forums like this. ^:)^

I wanted to share my solution, not only so that others trying the same thing can benefit from these open forum discussions, but so that other more savvy programmers might offer even BETTER approaches. For example, it might be worth trying the aforementioned Swing Timer class. Enjoy!

This is the class which implements the running timing thread; it updates the text of a public static jTextField in another class; the GregorianCalendar import is not used for this code but I have it as a placeholder for further efforts:

Re: Hello! Trying to Get Java Swing GUI to display clock/timer in jTextField

You got lucky because the setText() method is Thread safe, but when dealing with the GUI, you don't want to make changes to it on a Thread other than the EDT. A Swing Timer would have made this a non-issue, or you could use SwingUtilities to execute the code on the EDT.

January 28th, 2011, 01:13 AM

JavaNoob#5

Re: Hello! Trying to Get Java Swing GUI to display clock/timer in jTextField

Figured out how to use the Swing timer and listener. That IS a lot better and simpler to do; thanks!