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

Install Docker on Windows 11 with WSL Ubuntu 22.04

This is to install Docker within Ubuntu WSL without using the Windows Docker application. Follow the below steps. Install Ubuntu 22.04 WSL 1. Enable Windows Subsystem for Linux and Virtual Machine platform Go to Control Panel -> Programs -> Programs and Features -> Turn Windows features on or off 2. Switch to WSL 2 Open Powershell and type in the below command. wsl --set-default-version 2 If you don't have WSL 2, download the latest WSL 2 package and install it.  3. Install Ubuntu Open Microsoft Store and search for Ubuntu. Select the version you intend to install. I'd use the latest LTS version Ubuntu 22.04. Click on the Get button. It will take a couple of minutes to download and install. 4. Open up the installed Ubuntu version that was installed. If you get an error like the below image, make sure to install the WSL2 Kernel update .  If it's an older Ubuntu version the error message would be something like the image below. Error: WSL 2 requires an update to its ...

How to fix SSLHandshakeException PKIX path building failed in Java

TL ; DR 1. Extract the public certificate of the website/API that you are trying to connect from your Java application. Steps are mentioned in this post 2. Use the Java keytool to install the extracted certificate into the "cacerts" file (Trust store) keytool -import -trustcacerts -alias <domain name> -file <public certificate>.cert -keystore /path_to_java_home/jre/lib/security/cacerts -storepass changeit 3. Restart your Java application Exception A typical exception stack trace would look like below. javax.net.ssl. SSLHandshakeException : sun.security.validator.ValidatorException: PKIX path building failed : sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1959) at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302) at sun.security.ssl.Handshake...

Automatically open Chrome developer tools in a new tab

Sometimes we need to check the console or the network transactions of a link that opens up in a new tab. By default, the Chrome developer tools are not opening in a new tab. So, by the time when we hit F12 and open the dev tools, part of the information we needed could be already gone.  There's a setting in dev tools where you can keep the dev tools open automatically in a new tab. To enable that, hit F12 and open up the dev tools. Click on the settings icon in the top right corner. In the Preferences section, scroll down to the bottom. You'll be able to find the option to Auto-open DevTools for popups. Select the checkbox and we're good to go!