Java GUI: Guide to Data Validation

What is Data Validation?

Data Validation is the process of ensuring that a program operates on clean, correct and useful data. It uses routines, often called “validation rules” or “check routines”, that check for correctness, meaningfulness, and security of data that are input to the system.
Example – A name text field should not allow user to enter numbers or characters such as ‘+’,’=’,”&’, etc.
Mobile numbers should only include digits and ‘+’ sign.

Ensuring Data Validation – Important Points

♦ A good database structure – Database structure should be correct and flexible enough. Constraints like primary key and Not Null must be applied wherever necessary. Eg- Primary Key must applied to column like Admission_number and Not Null for column like Name.

♦ Minimum Input – A front-end program must only require minimum input from the user. Avoid asking for unnecessary data. Also, once an input is received don’t ask for the same data again.

♦ Choose the right component – Always implement the best GUI component possible for the particular type of data. Eg- When asking for class and section, provide a list to choose class and radio-buttons for choosing the section. This way user has the options only to enter valid data.

♦ No Ambiguity – There should not be any ambiguity(unclearness) in data and information required and the program should avoid inputting duplicate information anywhere in any form.

♦ Help user –  Program should help user to enter correct data in the first go. For this, each text-field must have a describing corresponding label. Tool tip text is also a good way to clarify user about the input required.

Implementing Data Validation (Java GUI)

Always show a message if a user enters any incorrect data. Write a catch statement to catch any exception that could occur during the execution of a program. Even show a message for successful completion of a task. You can put a label in the bottom of the program for this purpose or you can use JOptionPane to display messages.

Checking for ‘No Input’
Users often miss out to enter data in some fields or sometimes they assume a necessary field to be an optional one.
So, a program must check if the user has entered the required data or not. If the user has not provided the required data the program must not run further.
Checking for empty textfields – You can check if the textfield is empty or not by the following coding:

if (jTextField1.getText().isEmpty() || jTextField2.getText().isEmpty())
        JOptionPane.showMessageDialog(null, “One of the required field is empty!”, “Error”, JOptionPane.ERROR_MESSAGE);

or you can use the following method:

if (jTextField1.getText().length()==0 || jTextField2.getText().length()==0)

        JOptionPane.showMessageDialog(null, “One of the required field is empty!”, “Error”, JOptionPane.ERROR_MESSAGE);

Here, jTextField1 and jTextField2 are the fields which cannot be left empty. You can also notify user about the field that has been left empty by using if..else construct. The same coding, will work for jTextArea and jPasswordField.

Checking for ‘No selection from list’ 
Another run-time error occurs if the user does not select any value from the list component. User must be notified for the same.

if (jList1.getSelectedIndex()==-1)
       JOptionPane.showMessageDialog(null, “Please select a value from list”);

This will show a message to user for a null value from list i.e no value selected by user.

Checking for ‘No selection from Radio buttons’ 
Checking if user has not selected any choice from a group of radio buttons.
Remember, always put radio buttons in a button group, otherwise all radio buttons could be selected at a particular time.

if (jRadioButton1.isSelected()==false && jRadioButton2.isSelected()==false)
        JOptionPane.showMessageDialog(null, “Please Select your Gender!”);

or you can try the following code:

 if (jRadioButton1.isSelected())

    //Instead do your functioning!
    else if (jRadioButton2.isSelected())
    //Instead do your functioning!
        JOptionPane.showMessageDialog(null, “Please Select your Gender!”);

You can use the same piece of code for checkbox.

Prohibiting characters in numbers field
This code will help you to prohibit users from entering characters other than digits in numbers field like Moblie Number.
Write the following code on the KeyTyped event of a text field.

char k=evt.getKeyChar();
if (!(k>=’0′ && k<=’9′))

Now, if a user types characters other than digits, the event will be consumed.
The below coding will prevent user from typing digits in a field placed for characters(eg- Name field)

char k=evt.getKeyChar();
if (k>=’0′ && k<=’9′)

Prohibiting multiple selection in a list
Set the selectionMode property of list to Single_Selection by the following code to prevent user from selecting multiple items from list. Default is Multiple_Interval_Selection.



Also, each GUI form must have a Exit and Clear button.

Exit Button –


Clear Button –
Clearing TextFields


Clearing selection from list


Clearing selection from Combo Box


Clearing selection from Radio Buttons


Clearing the contents of a table

DefaultTableModel model=(DefaultTableModel) jTable1.getModel();
    int rows=model.getRowCount();
    if (rows>0){
       for(int j=0; j<rows; j++){

I hope this post will help you to implement a better data validation system for your java application.
I have tried to cover as much as possible but, if you want me to add more then please comment!!


6 thoughts on “Java GUI: Guide to Data Validation”

  1. In computer science, data validation is the process of ensuring that a program operates on clean, correct and useful data. It uses routines, often called "validation rules" or "check routines", that check for correctness, meaningfulness, and security of data that are input to the system. The rules may be implemented through the automated facilities of a data dictionary, or by the inclusion of explicit application program validation logic.

Leave a Comment

Your email address will not be published. Required fields are marked *