I need to make a JFormattedTextField following the format "00:00" with some requirements:
- The only changeable should be either of the "00". (So ":" should not be deletable)
- Tab makes you change between the two sides of the ":". (So having the cursor on one side and tabbing marks both "00" on the other side)
- Changing "00" to "2" should format it to "02".
- It's character limit should be 5 including the ":". (4 changeable characters)
- It should be initialized as "00:00", but it shouldn't be an acceptable input.
- You shouldn't be able to type in anything else than numbers. (Letters, symbols, negative numbers etc.)
Is there a way to do this? I have looked at different formatters, validators and Document Filter to add to the JFormattedTextField, but I'm not sure which ones to go with. (Using a DefaultFormatter right now, but have looked at a NumberFormatter for the limit. Will I need to use a combination of formatters and validators?)
This is my JFormattedTextField right now: http://pastebin.com/jW2RSJXe[1] .
Anything from code that does it to examples/pointers on what to look will be appreciated!
Run this example. It uses a
MaskFormatter
. The:
is there permanently, But I'm not sure how to format the other part of your question where if you only have 2, it'll show 02. You can play around with itThe
MaskFormatter
sets the type value for each character at each position and the size allowed. In this case, I used##:##
. Allowing two digits, a colon, and two more digits.Update: Added
formatText.setText("00:00");
to code to initialize the text field.