Question

I have a small issue with my updateButtonResults button. I have JOptionPane Message Dialogs that are programmed to pop up when the user updates the four fields First Name, Last Name, E-mail and Sign-up date. My problem is all 4 messages pop up, even if I only update one field. Example: I update a customers last name, the message dialogs will pop up in this order (First name, Last name, E-mail, Sign-up date).

Here is my code

//method for buttons on 'resultFrame'
public void BtnAction3() 
{                        
  updateButtonResults.addActionListener(
        new ActionListener()
        {
           //method for events that will be performed when updateButton is pressed
           public void actionPerformed(ActionEvent e) 
           {
              //instanciates variables to text in textfields
              String fname = fNameTextBoxResults.getText();
              String lname = lNameTextBoxResults.getText();
              String email = eMailTextBoxResults.getText();
              String signUpDate = signUpTextBoxResults.getText();

              try
              {
                 //statement that checks to make sure user enters only letters
                 if(fname.matches("[a-zA-Z]+"))
                 {
                    //updates 'Fname' field in db to text that user inputted in 'fname' textfield
                    rs2.updateString("Fname", fname);
                    JOptionPane.showMessageDialog(null, "Customer first name been updated!");
                 }

                 //statement that prompts user if they enter something other letters
                 else
                 {
                    JOptionPane.showMessageDialog(null, "Please enter first name in correct format!");
                    fNameTextBoxResults.setText("");
                 }

                 //statement that checks to make sure user enters only letters
                 if(lname.matches("[a-zA-Z]+"))
                 {   
                    //updates 'Lname' field in db to text that user inputted in 'lname' textfield                      
                    rs2.updateString("Lname", lname);
                    JOptionPane.showMessageDialog(null, "Customer last name been updated!");
                 }

                 //statement that prompts user if they enter something other letters
                 else
                 {
                    JOptionPane.showMessageDialog(null, "Please enter last name in correct format!");
                    lNameTextBoxResults.setText("");
                 }

                 //statement and actions if user enters a '.'
                 if(email.contains("."))
                 {  
                    //gets last period in "email"
                    int emailDotCheck = email.lastIndexOf(".");

                    //substring to period in variable "emailDotCheck"
                    String extensionCheck = email.substring(emailDotCheck);

                    //statement and actions if user doesn't enter email correctly           
                    if(!email.contains("@") || !extensionCheck.matches("\\.[a-z]{3}"))
                    {
                       JOptionPane.showMessageDialog(null, "Please enter email in correct format!");
                       eMailTextBoxResults.setText("");
                    }                   

                    //statement and actions if user enters email correctly
                    else
                    {
                       //updates 'E-mail' field in db to text that user inputted in 'email' textfield
                       rs2.updateString("E_mail", email);
                       JOptionPane.showMessageDialog(null, "Customer E-mail been updated!");
                    }
                 }

                 //action if user doesnt enter email correctly
                 else
                 {
                    JOptionPane.showMessageDialog(null, "Please enter email in correct format!");
                    eMailTextBoxResults.setText("");
                 }

                 //instance variables for 'signUpDate'
                 int month = 100;
                 int day = 100;
                 int year = 10000;

                 if(signUpDate.matches("\\d{2}/\\d{2}/\\d{4}"))
                 {
                    //instance variables
                    String monthStr = signUpDate.substring(0,2);
                    String dayStr = signUpDate.substring(3,5);
                    String yearStr = signUpDate.substring(6);

                    //parsing intstance variables to Integers
                    month = Integer.parseInt(monthStr);
                    day = Integer.parseInt(dayStr);
                    year = Integer.parseInt(yearStr);

                    //statement and actions if user doesn't follow correct format
                    if(month > 12 || day > 31 || year > 2100)
                    {
                       JOptionPane.showMessageDialog(null, "Please enter date in correct format! (dd/MM/yyyy)");
                       signUpTextBoxResults.setText("");
                    }

                    //statements and actions if user enters date correctly
                    else
                    {
                       //updates 'Sign-up date' field in db to text that user inputted in 'signUpDate' textfield
                       rs2.updateString("Sign_up_date", signUpDate);
                       JOptionPane.showMessageDialog(null, "Customer Sign-up date been updated!");
                    } 
                 }

                 //statement and actions if user doesn't follow correct format           
                 else 
                 {
                    JOptionPane.showMessageDialog(null, "Please enter date in correct format! (dd/MM/yyyy)");
                    signUpTextBoxResults.setText("");
                 }

                 //updates row in db
                 rs2.updateRow();
                 //JOptionPane.showMessageDialog(null, "Customer has been updated!");

              }

              catch(Exception ex)
              {

              }          
           }          
        });

I'm trying to learn to walk through my code, I have debugged it, but still couldn't figure the logic error out.

Thanks for any help

Was it helpful?

Solution

You have four text fields:

fNameTextBoxResults
lNameTextBoxResults
eMailTextBoxResults
signUpTextBoxResults

Rather than attempting to validate all of the input at once, let's try to make this code a little more modular. Separate all of the logic pertaining to a specific field and add it as an ActionListener to that field. Example:

 fNameTextBoxResults.addActionListener(
    new ActionListener()
    {
       public void actionPerformed(ActionEvent e) 
       {
             //statement that checks to make sure user enters only letters
             if(fname.matches("[a-zA-Z]+"))
             {
                //updates 'Fname' field in db to text that user inputted in 'fname' textfield
                rs2.updateString("Fname", fname);
                JOptionPane.showMessageDialog(null, "Customer first name been updated!");
             }

             //statement that prompts user if they enter something other letters
             else
             {
                JOptionPane.showMessageDialog(null, "Please enter first name in correct format!");
                fNameTextBoxResults.setText("");
             }
       }
    });

Rinse and repeat for the other three fields. If you have some finalizing type of action required, then you can do that with updateButtonResults. Otherwise, the button is unnecessary altogether.

OTHER TIPS

Instead of using an anonymous class that extends ActionListener, you might want to define a named class that extends it, and defines a constructor by which you could pass in data such as the previous values of the text fields:

class MyActionListener extends ActionListener {  // lousy name, but ...
    public MyActionListener(String prevFirstName, String prevLastName, String prevEMail, String prevSignUp) {
        this.prevFirstName = prevFirstName; ... and so on
    }
    public void ActionPerformed(ActionEvent e) {
        ... what you already have, except that you can say things like
        if (!fname.equals(prevFirstName)) {
            ... now you can do your dialog and it won't show up unless the names
            ... are different
        }
        ...
    }
}

and then set it like this:

public void BtnAction3() 
{                        
  updateButtonResults.addActionListener(
        new MyActionListener(firstName, lastName, eMail, signUp));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top