Skip to main content

Taking care of user inputs: Using jFormattedTextFields

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:
Character Description
#Any valid number (Character.isDigit).
'
(single quote)
Escape character, used to escape any of the special formatting characters.
UAny character (Character.isLetter). All lowercase letters are mapped to uppercase.
LAny character (Character.isLetter). All uppercase letters are mapped to lowercase.
AAny character or number (Character.isLetter or Character.isDigit).
?Any character (Character.isLetter).
*Anything.
HAny 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

Popular posts from this blog

VLC skins

It's been a while from my first blog post, so today I thought to make a blog post about skins in VLC player. I think you all are familiar with VLC player if not, here's the link for the website www.videolan.org/vlc/index.htm l In brief, VLC player is a free and opensource player that can play almost all the types of media formats. Even though it is a such a nice player, it looks kind of old and rusty, like the windows classic look. But you can make it prettier by just adding some new skins to it.  You can download skins for the player from here http://www.videolan.org/vlc/skins.php Then place the downloaded skin file (.vlt file) in the " skin " folder in the installation directory.           eg:- in windows:    C:\Program Files\VideoLAN\VLC\skins           or in linux systems:  ~/.local/share/vlc/skins Then, open the VLC player and go to the preferences. ...

Java, how to create a list with a single element

 I wanted to create a Java List with a single element. Yet, I wanted to add more elements later. So, I was looking for a couple of ways to do this. So far there are multiple elegant ways to create a list with a single element with a one-liner. Not so much for a modifiable list though. Here's what I gathered so far. Followings are a few ways of creating a list with strictly a single entry. Can't add more elements. 1. Collections.singletonList() This returns an immutable list that cannot be modified or add more elements. // An immutable list containing only the specified object. List<String> oneEntryList = Collections. singletonList ( "one" ) ; oneEntryList.add( "two" ) ; // throws UnsupportedOperationException 2.  Arrays.asList() This returns a fixed-size list consisting of the number of elements provided as arguments. The number of elements provided would be an array hence the size is fixed to the length of the array. // Returns a fixed-size list List...

Facebook videos are too loud

Not sure if it is only me, but as of this writing, I find some of the Facebook videos are too loud compared with other websites or players on the machine. Whenever I play a video, I usually turn the volume down. Then whenever there's an ad, it screams in my ears. Same when I play the next video, the former adjusted volume level doesn't stick. This could be a bug in FB, so I even suggested a feature improvement.  As a solution, I found this chrome plugin. Sound and video fixes for Facebook Don't know who developed it, but the dude has fixed one of Facebook's problems, and many thanks to the developer. I might as well help him out as it is an open-source project. No more screaming videos!