I've created two classes to eliminate de code bloat needed to create a JFormattedTextField that still does half the job.
The classes are: FormattedTextField and NumericTextField
What it does? Well, NumericTextField do:
1 - Not let the user input invalid characters in the field (e.g.: letters, and more than one decimal separator '.'). The input is verified at each key typed... so, it is always beautiful.
2 - Accept ActionEvent's properly (try to use them with JFormattedTextField and see what you get)
3 - Return the value as int, float, double, Integer, Float, Double and
String... an returns the integer and decimal portions (before '.' and after it)
4 - Let you put a text in front of the input and after it. Something like: U$, or ($ 123.00 canadian dollars), or 323%, and the cursor only acts after the text of the front and before the trailing text.
5 - Separate the thousands automatically (if you want). So when you type, the thousand separator is inserted as needed, while you are typing.
6 - Show the empty space that can be filled. (e.g. U$ ...,..), and you can set the character that marks the empty space
7 - IT JUST WORKS!!! And you don't need any code bloat to make it work.
And FormattedTextField do:
1 - Accept ActionEvent's (something that JFormattedTextField can't do)
2 - Not let you to jump spaces
3 - Return the text without the mask (if text is 12/12/2000, returns 12122000)
4 - Has a method that says if the field is totally filled
5 - Uses a, c and d as mask for alphanumeric chars, any character and digit chars, respectively. So, you can use a mask like: (#ddd) where ddd are three digits and # is just a character like /, -, whatever.
6- Show the mask if you want, or insert the mask while you type if you want
7 - Not need that you create a lot of code to do a simple thing.
Interested? Click
here and throw the JFormattedTextField into the trash can.
If you have any suggestion, opinion or criticism, mail me:
germanobax@yahoo.com.br