0% found this document useful (0 votes)
12 views4 pages

Data Validation in Java GUI

Data validation is the process of ensuring data is clean, correct and useful. It involves checking data for correctness, meaningfulness and security. Some key aspects of data validation are having a good database structure, minimizing unnecessary inputs, choosing appropriate input components, avoiding ambiguity, and helping users enter correct data. When implementing data validation in Java GUI programs, messages should be displayed for incorrect or missing data, empty fields, missing selections, and invalid character entries.

Uploaded by

bekalu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Data Validation in Java GUI

Data validation is the process of ensuring data is clean, correct and useful. It involves checking data for correctness, meaningfulness and security. Some key aspects of data validation are having a good database structure, minimizing unnecessary inputs, choosing appropriate input components, avoiding ambiguity, and helping users enter correct data. When implementing data validation in Java GUI programs, messages should be displayed for incorrect or missing data, empty fields, missing selections, and invalid character entries.

Uploaded by

bekalu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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)


Messages
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 ([Link]().isEmpty() ||
[Link]().isEmpty())
[Link](null, “One of the required
field is empty!”, “Error”, JOptionPane.ERROR_MESSAGE);

or you can use the following method:

if ([Link]().length()==0 ||
[Link]().length()==0)
[Link](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 ([Link]()==-1)
[Link](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 ([Link]()==false &&
[Link]()==false)
[Link](null, “Please Select your
Gender!”);

or you can try the following code:

if ([Link]())
gender=”Male”;
//Instead do your functioning!
else if ([Link]())
gender=”Female”;
//Instead do your functioning!
else
[Link](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=[Link]();
if (!(k>=’0′ && k<=’9′))
[Link]();

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=[Link]();
if (k>=’0′ && k<=’9′)
[Link]();

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.

[Link]([Link].SINGLE_SEL
ECTION);

Also, each GUI form must have a Exit and Clear button.
Exit Button –
[Link](0);

Clear Button –
Clearing TextFields
[Link](“”);
[Link](“”);
Clearing selection from list

[Link]();
Clearing selection from Combo Box

[Link](-1);
Clearing selection from Radio Buttons

[Link]();
Clearing the contents of a table

DefaultTableModel model=(DefaultTableModel) [Link]();


int rows=[Link]();
if (rows>0){
for(int j=0; j<rows; j++){
[Link](0);
}
}

You might also like