Search code examples
javaswingjtextfield

Check JTextField Given Data


I use this code to ensure that each given data from textfields are correct.

But in dateTextField, when I give it anything or leave it empty, its error message is shown, but when I click to save button, that data will be saved to my JTable!

Where is my mistake?

public class AddBookDialog extends javax.swing.JDialog {

public AddBookDialog(JFrame owner){
    super(owner,"New Book",true);
    ...
     }
BookInformation bookinform;

 private void OkButtonActionPerformed(java.awt.event.ActionEvent evt) { 
    bookinform=new BookInformation();

        if(!BookIDControl()){
            return;
        }

        if(!BookNameControl()){
            return;
        }

        if(!BookDateControl()){
            return;
        }

        bookinform.setBorrowStatus("No");

      AddBookDialog.this.dispose();

}

public BookInformation getBookInfos() throws NullPointerException{
    return bookinform;
}
    public boolean BookNameControl(){
       bookinform.setBookName(BookNametextfiled.getText());
    if(bookinform.getBookName().trim().isEmpty()){
        errorLable.setText("Empty Book Name");
        return false;
    }

    if(havedigit(bookinform.getBookName().trim())){
        errorLable.setText("Book Name Have Digit");
        return false;
        }
    return true;
    }

     public boolean BookDateControl(){
     bookinform.setBookDate(BookDatetextfield.getText().trim());
    if(bookinform.getBookDate().trim().isEmpty()){
        errorLable.setText("Empty Book Date");
        return false;
    }
       else if(haveSpace(bookinform.getBookDate().trim())){
        errorLable.setText("Space in Book Date!");
        return false;
    }

   else if(haveletter(bookinform.getBookDate().trim())){
        errorLable.setText("Letter in Book Date");
        return false;
    }
        return true;
    }

    public boolean BookIDControl(){
    bookinform.setBookID(BookidTextfield.getText().trim());
    if(bookinform.getBookID().trim().isEmpty()){
        errorLable.setText("Empty Book ID");
        return false;
    }

    if(haveSpace(bookinform.getBookID().trim())){
        errorLable.setText("Space in Book ID !");
        return false;
    }

    if(haveletter(bookinform.getBookID().trim())){
        errorLable.setText("Letter in Book ID");
        return false;
    }
    return true;
    }
...

Second Class:

public class AllBooksM extends JFrame implements ActionListener{
...
if(e.getSource()==AddBookButton){
    AllBooks allBooks=new AllBooks();
    AddBookDialog add_book=new AddBookDialog(AllBooksM.this);
    add_book.setVisible(true);
    BookInformation B_info=add_book.getBookInfos();
     if(B_info != null){
      allBooks.AddRow(B_info);
     }
 //    bookcount();

    }
...
    }

My Output :

26     thired     62     Yes
29     sixth     92     No
35     vff     53     No
34332     dsds          null
3434     ssdas          null
222     fgfgf          null
77     sds          null

Solution

  • Well the code below if "BookDatetextfield.getText()" is null, then every time you call the trim() method an exception will be thrown and if your app doesn't die it's because you're catching the exception.

        public boolean BookDateControl(){
            bookinform.setBookDate(BookDatetextfield.getText().trim());
    
            if(bookinform.getBookDate().trim().isEmpty()){
                errorLable.setText("Empty Book Date");
                return false;
            } else if(haveSpace(bookinform.getBookDate().trim())){
                errorLable.setText("Space in Book Date!");
                return false;
            } else if(haveletter(bookinform.getBookDate().trim())){
                errorLable.setText("Letter in Book Date");
                return false;
            }
    
            return true;
        }
    

    A work around is doing certain validations before if you have the spring.jar, they have some methods that are quite helpful, then your code will look like this:

       public boolean BookDateControl(){
            if(!StringUtils.hasLength(BookDatetextfield.getText()) || !StringUtils.hasText(BookDatetextfield.getText())){
                errorLable.setText("Empty Book Date");
                return false;
            }
            bookinform.setBookDate(BookDatetextfield.getText().trim());
    
            if(bookinform.getBookDate().trim().isEmpty()){
                errorLable.setText("Empty Book Date");
                return false;
            } else if(haveSpace(bookinform.getBookDate().trim())){
                errorLable.setText("Space in Book Date!");
                return false;
            } else if(haveletter(bookinform.getBookDate().trim())){
                errorLable.setText("Letter in Book Date");
                return false;
            }
    
            return true;
        }
    

    The method as length will return false if string is either null or zero length. The hasText will only return true if the string has more than blank spaces.