Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Component to display/edit a time interval length

814992Dec 20 2010 — edited Dec 22 2010
Hi all

I'm trying to decide what would be the best Swing component to take input of a length of time. Ideally I'd prefer to avoid having the user type in a formatted number (hh:mm:ss for example) that has to be validated and just have a component that will only show/allow valid input. I had envisaged using a JSpinner component, starting at "00:00:00" and letting the user 'spin' up to a number of hours, minutes and seconds. In fact the JSpinner would be ideal if I could work out how to get it to correctly display and modify this kind of value as opposed to date values. At the moment I have a JSpinner set up using a DateModel and DateEditor and initialised with a blank date. This works in that I can ignore all but the time values in the date object so the user can just define an amount of time 'since midnight' which I can effectively treat as the interval length. The trouble is that using a Date-configured spinner displays the time in a 12-hour range from 12:00:00, to 01:59:59, at no point does the display tick to 00:00:00 which, for my purposes, is what the user should start with as a value. So, to enter a time interval of, say, 30 minutes and 20 seconds, I currently have to set the spinner to 12:30:20, given this is 30 minutes and 20 second since midnight.

Ideally a time interval shouldn't be tied to a date object at all but if necessary, just a way of pursuading the Spinner to display times between midnight and 1am as starting 00 hours would be a way around the problem. The ideal solution would be a component that acts largely like the Spinner which holds a value behind the scenes as a long in seconds but formats that value into hh:mm:ss seconds for display and also doesn't have the 12 hour or 24 hour range limitation inherent in the Date formatted spinner. I suspect some manipulation of the Model and Editor being used by a JSpinner could achieve this but the Oracle article about JSpinners goes into little or no detail about what a custom editor can do and how etc.

So, does anyone know how I might achieve either of those ends, displaying time in a Date Spinner starting at 0 rather than 12 or customizing a spinner or other component to take a time interval as input.
This post has been answered by Jörg on Dec 20 2010
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 19 2011
Added on Dec 20 2010
7 comments
498 views