This is a proof of concept, designed to demonstrate a possible solution to initial requirements, which was used to gain feedback and drive actual development
Some time ago I had a client that was unimpressed with JFormattedTextField
and
JSpinner
when it came to entering in time values. Coming from a pre-existing
text based system, they wanted to be able to at least replicate the functionality
that the pre-existing system had, if not exceed it.
The field needed to provide:
- Formatting in
hh:mm a
orHH:mm
- Real time validation
- This would mean limiting what the user could enter based on a series of rules
including, if set to 12 hour time, if the user typed
1
in the hour field, they could only enter0
,1
or2
, with simular restrictions on the minutes and seconds fields
- This would mean limiting what the user could enter based on a series of rules
including, if set to 12 hour time, if the user typed
- Simple navigation, moving from the hours portion to the seconds portion when
the user exceeds the number of digits for the field (ie typing
03
in the hour portion should move the user to the minutes portion without them needing to do anything), they should also be able to arrow key back and forwards like a normal field, moving between the different time portions.
The implementation also provided
- The ability to [Tab] between field portions
- Use the [:] to move from the hour to seconds portions, which allowed for a
more natural typing experience (
1:52
for example)
A further enhancement was made later to introduce a concept of a DurationField
which allowed the entry of any number of hours, but which still restricted the
minutes (and potentially seconds)
The code is intended to demonstrate the possibilities for a time/duration field based on some basic requirements.
Future enhancements would include:
- The ability to configure seconds and milliseconds
- The ability to change the time separators based on localization