-1
private void loadActionPerformed(java.awt.event.ActionEvent evt) {                                     
        validation();
        CreateXml();
        clear();

  public void validation(){
    if (username.getText().trim().length()==0).v{

        JOptionPane.showMessageDialog(Retrycount, "Please input user name", "ERROR", JOptionPane.OK_CANCEL_OPTION);
    }


    if (PASSWORD1.getText().length()==0){

        JOptionPane.showMessageDialog(Retrycount, "Please input password","ERROR", JOptionPane.OK_CANCEL_OPTION);
    }
    if(DOMAIN1.getText().length()==0){
         JOptionPane.showMessageDialog(Retrycount, "Please input Domain name","ERROR", JOptionPane.OK_CANCEL_OPTION);

    }
     if (MYSQLPassword.getText().length() ==0){
         JOptionPane.showMessageDialog(Retrycount, "Please input MYSQLPassword","ERROR", JOptionPane.OK_CANCEL_OPTION);

     }
     if (Username_Domain.getText().length() ==0){
         JOptionPane.showMessageDialog(Retrycount, "Please input second username under FTPFileUploaderPatch","ERROR", JOptionPane.OK_CANCEL_OPTION);
     }
     if (Password2.getText().length() ==0){
         JOptionPane.showMessageDialog(Retrycount, "Please input Password under FTPFileUploaderPatch","ERROR", JOptionPane.OK_CANCEL_OPTION);
     }

     if (DestinationType1.getText().length() ==0){
         JOptionPane.showMessageDialog(Retrycount, "Please input DestinationType under FTPFileUploaderPatch","ERROR", JOptionPane.OK_CANCEL_OPTION);

     }
     if (DestinationFileName1.getText().length() ==0){
        JOptionPane.showMessageDialog(Retrycount, "Please input DestinationFileName under FTPFileUploaderPatch","ERROR", JOptionPane.OK_CANCEL_OPTION);      
    }

     if (DestinationEntry1.getText().length() ==0){
         JOptionPane.showMessageDialog(Retrycount, "Please input DestinationEntry under FTPFileUploaderPatch","ERROR", JOptionPane.OK_CANCEL_OPTION);

     }
     if (SubDirectorySearchString1.getText().length() ==0) {
         JOptionPane.showMessageDialog(Retrycount, "Please input DirectorySearchString under FTPFileUploaderPatch","ERROR", JOptionPane.OK_CANCEL_OPTION);
     }
     if (TempDirectory1.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input TempDirectory under FTPFileUploaderPatch","ERROR", JOptionPane.OK_CANCEL_OPTION);
     }

     if (SourcePath2.getText().length() ==0 ) {JOptionPane.showMessageDialog(Retrycount, "Please input SourcePath2 under FTPFileUploaderPatch","ERROR", JOptionPane.OK_CANCEL_OPTION);

     }

     if (DestinationType2.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input DestinationType2 under FTPFileUploaderPatch > ReplicationMonitor","ERROR", JOptionPane.OK_CANCEL_OPTION); 
     }
    if (DestinationType2.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input DestinationPath2 under FTPFileUploaderPatch > ReplicationMonitor","ERROR", JOptionPane.OK_CANCEL_OPTION); 
    }
    if (DestinationRootFolder2.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input DestinationRootFolder2 under FTPFileUploaderPatch > ReplicationMonitor","ERROR", JOptionPane.OK_CANCEL_OPTION); 
  } 
    if (DestinationFileName2.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input DestinationFileName2 under FTPFileUploaderPatch > ReplicationMonitor","ERROR", JOptionPane.OK_CANCEL_OPTION); 
  }
    if (DestinationEntry2.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input DestinationEntry2 under FTPFileUploaderPatch > ReplicationMonitor","ERROR", JOptionPane.OK_CANCEL_OPTION); 
    } 
     if (SubDirectorySearchString2.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input SubDirectorySearchString2 under FTPFileUploaderPatch > ReplicationMonitor","ERROR", JOptionPane.OK_CANCEL_OPTION); 
     }
     if (TempDirectory2.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input TempDirectory2 under FTPFileUploaderPatch > ReplicationMonitor","ERROR", JOptionPane.OK_CANCEL_OPTION); 
     }
     if (SourcePath3.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input SourcePath3 under FTPFileUploaderPatch > Config.properties","ERROR", JOptionPane.OK_CANCEL_OPTION); 
     }
     if (DestinationType3.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input DestinationType3 under FTPFileUploaderPatch > Config.properties","ERROR", JOptionPane.OK_CANCEL_OPTION); 
     }
    if (DestinationPath3.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input DestinationPath3 under FTPFileUploaderPatch > Config.propertiesr","ERROR", JOptionPane.OK_CANCEL_OPTION); 
    }
    if (DestinationRootFolder3.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input DestinationRootFolder3 under FTPFileUploaderPatch > Config.properties","ERROR", JOptionPane.OK_CANCEL_OPTION); 
    }
   if (DestinationFileName3.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input DestinationFileName3 under FTPFileUploaderPatch > Config.properties","ERROR", JOptionPane.OK_CANCEL_OPTION); 
   }
 if (DestinationEntry3.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input DestinationEntry3 under FTPFileUploaderPatch > Config.properties","ERROR", JOptionPane.OK_CANCEL_OPTION); 
 }

The problem is the validation I need it, to stop the validation after the first text box and give it attention. If the first text field is empty, and you have pressed the validate button the cursor should go there and not continue validating the rest.

How to achieve that goal?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Please learn common [Java naming conventions](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307) (specifically the case used for the names) for class, method & attribute names & use them consistently. – Andrew Thompson Jul 22 '13 at 14:24
  • `PASSWORD1.getText()` will return a `String` which might can lead to security breach. Consider keeping passwords as `char[]` array, and as soon as you are done with it over write their content using something like `Arrays.fill(passwordArray, ' ')`, so that no one can access it easily. – nIcE cOw Jul 22 '13 at 14:34
  • This thread regarding [Drawing warning symbols during form validation in Swing](http://stackoverflow.com/q/14170257/1057230), might will definitely interest you :-) – nIcE cOw Jul 22 '13 at 16:42

2 Answers2

3

Instead of writing

 if (PASSWORD1.getText().length()==0){
     JOptionPane.showMessageDialog(Retrycount, "Please input password","ERROR", JOptionPane.OK_CANCEL_OPTION);
}
if(DOMAIN1.getText().length()==0){
     JOptionPane.showMessageDialog(Retrycount, "Please input Domain name","ERROR", JOptionPane.OK_CANCEL_OPTION);
}

and so on, use the else if construction:

if (PASSWORD1.getText().length()==0){
    JOptionPane.showMessageDialog(Retrycount, "Please input password","ERROR", JOptionPane.OK_CANCEL_OPTION);
} else if(DOMAIN1.getText().length()==0){
    JOptionPane.showMessageDialog(Retrycount, "Please input Domain name","ERROR", JOptionPane.OK_CANCEL_OPTION);
}
mthmulders
  • 9,483
  • 4
  • 37
  • 54
2

After each field validation set the focus to that field them exit from the function.

Like (not confident about the function names);

if (username.getText().trim().length()==0).v{
    JOptionPane.showMessageDialog(Retrycount, "Please input user name", "ERROR", JOptionPane.OK_CANCEL_OPTION);
    username.getFocus();
    return;
}
WattoWatto
  • 159
  • 8
  • 1
    I guess you mean to say [JComponent.requestFocusInWindow()](http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#requestFocusInWindow()) +1, this approach can work too :-) – nIcE cOw Jul 22 '13 at 14:36