When developing an application, it is critical to validate the user inputs. In order to give the right output the right input should be given. But the users don't always provide the right inputs as we expect, the right design should have the ability to deal with all kinds of bizarre inputs that user might give in.
One way to handle them is validating the given inputs before we process it.
But it is better to limiting the user to type something we don't expect.
In Java swing we can use formatted text fields for this task.
This is a quick tutorial for jFormattedTextFields using Netbeans. This will be enough to get it done, and I won't go more details on it.
Formatted text field is very similar to the simple jTextField but we can set a format that it would only accept as an input. Therefor user will not be able to type anything else other than the given format.
First let's look doing that easily using Netbeans IDE.
Netbeans provides jFormattedTextField in the tool box.
We can simply drag and drop it to a jFrame.
Now let's see how to format the inputs.
To edit the format of the text field, right click on it and go to the properties window.
In the properties window there is a property called formatterFactory
Click on the button to edit the format factory.
There are few pre-defined formats that we can use generally.
We can use them or else we can make our own format.
To make our own format select the mask from the category above.
And select the custom from the Format.
In the Format field in the right, we can type in the format we want to set.
and it provides a field to test the format with some inputs in the Example region.
The following table shows the characters that you can use in the formatting mask:
One way to handle them is validating the given inputs before we process it.
But it is better to limiting the user to type something we don't expect.
In Java swing we can use formatted text fields for this task.
This is a quick tutorial for jFormattedTextFields using Netbeans. This will be enough to get it done, and I won't go more details on it.
Formatted text field is very similar to the simple jTextField but we can set a format that it would only accept as an input. Therefor user will not be able to type anything else other than the given format.
First let's look doing that easily using Netbeans IDE.
Netbeans provides jFormattedTextField in the tool box.
We can simply drag and drop it to a jFrame.
Now let's see how to format the inputs.
To edit the format of the text field, right click on it and go to the properties window.
In the properties window there is a property called formatterFactory
Click on the button to edit the format factory.
There are few pre-defined formats that we can use generally.
We can use them or else we can make our own format.
To make our own format select the mask from the category above.
And select the custom from the Format.
In the Format field in the right, we can type in the format we want to set.
and it provides a field to test the format with some inputs in the Example region.
The following table shows the characters that you can use in the formatting mask:
Character | Description |
---|---|
# | Any valid number (Character.isDigit ). |
' (single quote) | Escape character, used to escape any of the special formatting characters. |
U | Any character (Character.isLetter ). All lowercase letters are mapped to uppercase. |
L | Any character (Character.isLetter ). All uppercase letters are mapped to lowercase. |
A | Any character or number (Character.isLetter or Character.isDigit ). |
? | Any character (Character.isLetter ). |
* | Anything. |
H | Any hex character (0-9, a-f or A-F). |
For an example let's try a number format such as
UWU/CST/EX/0001
here, UWU and EX never change, CST can have any 3 uppercase letters and 0001 can have any 4 digits.
the first portion "UWU/" must be constant for all the numbers.
To say that, in the format field we type 'UW'U/
since "U" is used to define an upper case character, we cannot use it as it is to say there should be a "U".
if we only type U, as to the above table it means there can be any upper case letters. But we need it only to be upper case U. Therefore we put the single quote escape character before the U letter.
Next portion of our original format is CST/
CST can be any 3 upper case letters
we can say that with 'UW'U/UUU/
Next portion is EX/
Again they should be constant.
But we don't have any of them in the above table. Therefore we can use them straight away.
now our format is 'UW'U/UUU/EX/
Next portion is 0001
That can be any four digits.
To represent a digit we use # symbol
Now our format is 'UW'U/UUU/EX/####
Note that to say there should be a constant "/", we used it straight away. Simply to define a constant value, we can use that character straight away unless it is not included in the above table. If it is in the table it means something else. If so we have to escape that meaning by adding the escape character ( ' ) single quote before the particular character in the format.
Then we can click OK to close the window and test our formatted text field.
In our final jFrame, the constant values show as they are. It has blank spaces where we have to type in the other part of the number.
You can read more about this in the original documentation here.
Comments
Post a Comment