User Interface input Data Validation in Swing

 

The Simple Validation is used  for quickly adding validation code to Swing user-interface.The User Interface needs to make sure that what the user typed is legal, if it is not then  the user should be shown the error or problem.

 

 

 

 

//User Interface input Data Validation in Swing

import java.util.Date;

import java.util.Calendar;

import java.util.GregorianCalendar;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class CustomerApplet1 extends JApplet implements Runnable

{

                //Variable for the panel

                JPanel panelObj;

        Thread datimeThrad;

        Date datee;

        GregorianCalendar calendarr;

        String strdatee, strTimee, strStatuss;

      //variables for labels

                JLabel labellCustName;

                JLabel labellCustCellNo;

                JLabel labellCustPackage;

                JLabel labellCustAge;

                //variables for data entry controls

                JTextField txtCustName; 

                JTextField txtCustCellNo;

                JComboBox comboCustPackag;

                JTextField txtCustAge;

                JButton bttnAccept = new JButton("Accept");

                //Variables for the layout

                GridBagLayout gbgObject;

                GridBagConstraints gbconst;

                public void init()

                {

                                //Initialize the layout variables

                                gbgObject = new GridBagLayout();

                                gbconst = new GridBagConstraints();

                                panelObj = (JPanel)getContentPane();

                                panelObj.setLayout(gbgObject);

           

            //Initialize label controls

                                labellCustName = new JLabel("Customer Name");

                                labellCustCellNo = new JLabel("Cell Number");

                                labellCustPackage = new JLabel("Package");

                                labellCustAge = new JLabel("Age");

                                //Initialize data entry controls

                                txtCustName = new JTextField(30);

                                txtCustCellNo = new JTextField(15);

                                txtCustAge = new JTextField(2);

                                String packages[] = { "Executive", "Standard"};

                                comboCustPackag = new JComboBox(packages);

                               

                                //Add controls for Customer Name

                                gbconst.anchor = GridBagConstraints.NORTHWEST;

                                gbconst.gridx = 1;

                                gbconst.gridy = 5;

                                gbgObject.setConstraints(labellCustName,gbconst);

                                panelObj.add(labellCustName);

               

                                gbconst.anchor = GridBagConstraints.NORTHWEST;

                                gbconst.gridx = 4;

                                gbconst.gridy = 5;

                                gbgObject.setConstraints(txtCustName,gbconst);

                                panelObj.add(txtCustName);

                                //Add controls for Cell Number

                                gbconst.anchor = GridBagConstraints.NORTHWEST;       

                                gbconst.gridx = 1;

                                gbconst.gridy = 8;

                                gbgObject.setConstraints(labellCustCellNo,gbconst);

                                panelObj.add(labellCustCellNo);

                                gbconst.anchor = GridBagConstraints.NORTHWEST;

                                gbconst.gridx = 4;

                                gbconst.gridy = 8;

                                gbgObject.setConstraints(txtCustCellNo,gbconst);

                                panelObj.add(txtCustCellNo);

                                //Add controls for Package

                                gbconst.anchor = GridBagConstraints.NORTHWEST;

                                gbconst.gridx = 1;

                                gbconst.gridy = 11;

                                gbgObject.setConstraints(labellCustPackage,gbconst);

                                panelObj.add(labellCustPackage);

                                gbconst.anchor = GridBagConstraints.NORTHWEST;

                                gbconst.gridx = 4;

                                gbconst.gridy = 11;

                                gbgObject.setConstraints(comboCustPackag,gbconst);

                                panelObj.add(comboCustPackag);

            //Add controls for Customer Age

                                gbconst.anchor = GridBagConstraints.NORTHWEST;

                                gbconst.gridx = 1;

                                gbconst.gridy = 14;

                                gbgObject.setConstraints(labellCustAge,gbconst);

                                panelObj.add(labellCustAge);

                                gbconst.anchor = GridBagConstraints.NORTHWEST;

                                gbconst.gridx = 4;

                                gbconst.gridy = 14;

                                gbgObject.setConstraints(txtCustAge,gbconst);

                                panelObj.add(txtCustAge);

                               

                                gbconst.anchor = GridBagConstraints.NORTHEAST;

                                gbconst.gridx = 5;

                                gbconst.gridy = 17;

                                gbgObject.setConstraints(bttnAccept,gbconst);

                                panelObj.add(bttnAccept);

                                ValidateAction validateButton = new ValidateAction();

                                bttnAccept.addActionListener(validateButton);               

                               datimeThrad = new Thread(this); //Initialize thread

                              datimeThrad.start(); //Starting thread

                }

      public void run() // body of the thread

      {

        while(datimeThrad != null)

        {

                displaay();

                try

                {

                     datimeThrad.sleep(1000);

                }

                catch(InterruptedException e)

                {

                     getAppletContext().showStatus("Oh!Thread interrupted");

                }

        }

      }

      public void displaay() //displaays date and time on the status bar

      {                    

         datee = new Date();

         calendarr = new GregorianCalendar();

         calendarr.setTime(datee);

         strTimee =   calendarr.get(calendarr.HOUR)+":"+calendarr.get(calendarr.MINUTE)+":"+calendarr.get(calendarr.SECOND);

         strdatee = (calendarr.get(calendarr.MONTH)+1)+"/"+calendarr.get(calendarr.DATE)+"/"+calendarr.get(calendarr.YEAR);

         strStatuss=strTimee+"      "+strdatee;

         getAppletContext().showStatus(strStatuss);

            }

     class ValidateAction implements ActionListener

     {

                public void actionPerformed(ActionEvent acevt)

                {

                                Object obj = acevt.getSource();

                                if(obj == bttnAccept)

                                {

                                                String customerMobNo = txtCustCellNo.getText();

                                                if(customerMobNo.length() == 0)

                                                {

                                                   getAppletContext().showStatus("MobNo.cannot be empty");

                                                                return;

                                                }

                                      String customerNamee = txtCustName.getText();

                                      if(customerNamee.length() == 0)

                                                {

                                                      getAppletContext().showStatus("Name cannot be empty");

                                                                return;

                                                }

                                                int customerAg = Integer.parseInt(txtCustAge.getText());

                                                if(customerAg <= 0 || customerAg >= 100)

                                                {

                                                       getAppletContext().showStatus("Invalid value for age");

                                                       return;

                                                }             

                                   }

                        }

              }

}

/* <applet code="CustomerApplet1.class" height = 400 width = 400>

</applet>

*/

Output......

data validation