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...

Ubuntu DNS issue fix DNS_PROBE_FINISHED_BAD_CONFIG

Issue  I've been playing with a VPN and somehow it messed up my DNS resolution configurations. Chrome gives  DNS_PROBE_FINISHED_BAD_CONFIG  error and can't ping google. So it seemed to be an issue with the DNS. Of course, restarting didn't fix it. I tried DNS lookup which gave me below. To make sure this is somehting to do with my DNS confgis, I ran the same by providing the google DNS servers.  It works, which means my default DNS is not working for some reason. To make sure this, ran the below command. systemd-resolve --status Output has an entry for DNS Servers, which was  ::1 Fix 1. Edit the file /etc/systemd/resolved.conf. sudo vi /etc/systemd/resolved.conf 2. Add new DNS entries. I added 2 google DNS and the cloudflare DNS sever. [Resolve] DNS=8.8.8.8 8.8.4.4 1.1.1.1 3. Restart the systemd-resolved and check the configuration is persisted in /run/systemd/resolve/resolv.conf file. sudo service systemd-resolved restart cat /run/systemd/resolve/resolv.co...