Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default Help with Action Performed method

    I'm working on an assignment, and I have most of the code written, but I'm lost now.

    I'm supposed to adjust the if statement in actionperformed method to cause execution of the methods: checkphone, checkemail, and checkpassword.
    the checkphone, checkemail, and checkpassword methods need to be added to the program. The checkphone method should test that the user has entered the first 3 digits of the phone as 469.
    checkemail method should test that the user has entered an @ sign somewhere in the email field.

    Any help would be appreciated

    Here's what I have so far:
    Java Code:
    import java.awt.*;	//import older gui library for content pane
    import javax.swing.*;	//import newer gui library for labels, textfields, and button
    import java.awt.event.*; //import gui event action library
    
    public class RegistrationForm extends JFrame implements ActionListener {
    
        // declare labels used on GUI screen
        private JLabel labelName, labelAddress, labelCity, labelState, labelZip, labelPhone, labelEmail, labelPassword, labelRetypePassword;
    
        private JLabel labelError, labelRegistration;  // used for good and bad messages
        // declare text fields used on GUI screen
        private JTextField textName, textAddress, textCity, textState, textZip, textPhone, textEmail, textPassword, textRetypePassword;
        // declare button used on GUI screen
        private JButton submitButton, clearButton;
        // declare content pane
        private Container contentPane;
    
        public RegistrationForm() {
            createGUI();
        } // ends  constructor
    
        private void createGUI() {
            try {
                // get content pane and set the layout to null
                contentPane = getContentPane();
                contentPane.setLayout(null);	//free-form layout
                setFont(new Font("TimesRoman", Font.ITALIC, 14));
    
                // create the name label
                labelName = new JLabel();	//instantiate new JLabel
                labelName.setText("Name");	//set label text to name
                labelName.setLocation(38, 10);	//set location of JLabel
                labelName.setSize(200, 25);	//set size of JLabel
                labelName.setForeground(Color.BLACK);//set initial background color
                contentPane.add(labelName);	//add JLabel to content pane
    
                // create the name text box
                textName = new JTextField();	//instantiate new JTextField
                textName.setText("");	//clear JTextField
                textName.setToolTipText("Please type in full name - last name first");
                textName.setLocation(75, 10);	//set location of JTextFfield
                textName.setSize(200, 25);   //set size of JTextField
                contentPane.add(textName); //add jextfield to content pane
    
                // create the address label
                labelAddress = new JLabel();
                labelAddress.setText("Address");
                labelAddress.setLocation(23, 50);
                labelAddress.setSize(80, 25);
                labelAddress.setForeground(Color.BLACK);
                contentPane.add(labelAddress);
    
                // create the address text box
                textAddress = new JTextField();
                textAddress.setText("");
                textAddress.setToolTipText("Please type in full address");
                textAddress.setLocation(75, 50);
                textAddress.setSize(300, 25);
                contentPane.add(textAddress);
    
                // create the city label
                labelCity = new JLabel();
                labelCity.setText("City");
                labelCity.setLocation(48, 90);
                labelCity.setSize(300, 25);
                labelCity.setForeground(Color.BLACK);
                contentPane.add(labelCity);
    
                // create the city text box
                textCity = new JTextField();
                textCity.setText("");
                textCity.setToolTipText("Please type in full city");
                textCity.setLocation(75, 90);
                textCity.setSize(130, 25);
                contentPane.add(textCity);
    
                // create the state label
                labelState = new JLabel();
                labelState.setText("State");
                labelState.setLocation(40, 130);
                labelState.setSize(300, 25);
                labelState.setForeground(Color.BLACK);
                contentPane.add(labelState);
    
                // create the state text box
                textState = new JTextField();
                textState.setText("");
                textState.setToolTipText("Choose State");
                textState.setLocation(75, 130);
                textState.setSize(130, 25);
                contentPane.add(textState);
    
                // create the zip label
                labelZip = new JLabel();
                labelZip.setText("Zip Code");
                labelZip.setLocation(23, 170);
                labelZip.setSize(300, 25);
                labelZip.setForeground(Color.BLACK);
                contentPane.add(labelZip);
    
                // create the zip text box
                textZip = new JTextField();
                textZip.setText("");
                textZip.setToolTipText("Zip code must be between 10000 and 99999 inclusive");
                textZip.setLocation(75, 170);
                textZip.setSize(300, 25);
                contentPane.add(textZip);
    
                // create the phone label
                labelPhone = new JLabel();
                labelPhone.setText("Phone");
                labelPhone.setLocation(25, 270);
                labelPhone.setSize(200, 25);
                labelPhone.setForeground(Color.BLACK);
                contentPane.add(labelPhone);
    
                  // create the phone text box
                textPhone = new JTextField();
                textPhone.setText("");
                textPhone.setToolTipText("type in phone number");
                textPhone.setLocation(75, 270);
                textPhone.setSize(130, 25);
                contentPane.add(textPhone);
    
                 // create the email label
                labelEmail = new JLabel();
                labelEmail.setText("Email");
                labelEmail.setLocation(25, 370);
                labelEmail.setSize(200, 25);
                labelEmail.setForeground(Color.BLACK);
                contentPane.add(labelEmail);
    
                  // create the email text box
                textPhone = new JTextField();
                textPhone.setText("");
                textPhone.setToolTipText("Enter email address");
                textPhone.setLocation(75, 370);
                textPhone.setSize(130, 25);
                contentPane.add(textPhone);
    
                 // create the password label
                labelPassword = new JLabel();
                labelPassword.setText("Password");
                labelPassword.setLocation(250, 270);
                labelPassword.setSize(200, 25);
                labelPassword.setForeground(Color.BLACK);
                contentPane.add(labelPassword);
    
                   //create the password text box
                textPassword = new JTextField();
                textPassword.setText("");
                textPassword.setToolTipText("Enter email address");
                textPassword.setLocation(315, 270);
                textPassword.setSize(130, 25);
                contentPane.add(textPassword);
    
                   // create the Retypepassword label
                labelRetypePassword = new JLabel();
                labelRetypePassword.setText("Retype Password");
                labelRetypePassword.setLocation(220, 370);
                labelRetypePassword.setSize(200, 25);
                labelRetypePassword.setForeground(Color.BLACK);
                contentPane.add(labelRetypePassword);
    
                   //create the email text box
                textRetypePassword = new JTextField();
                textRetypePassword.setText("");
                textRetypePassword.setToolTipText("Reenter Password");
                textRetypePassword.setLocation(330, 370);
                textRetypePassword.setSize(130, 25);
                contentPane.add(textRetypePassword);
               
               
         
    
                // create the submit button
                submitButton = new JButton();
                submitButton.setText("Submit");
                submitButton.setToolTipText("Click \"submit \" when the form is completely filled out");
                submitButton.setLocation(125, 450);
                submitButton.setSize(100, 30);
                contentPane.add(submitButton);
                submitButton.addActionListener(this);
    
                clearButton = new JButton();
                clearButton.setText("Clear");
                clearButton.setToolTipText("Click \"clear \" when you want to clear the form");
                clearButton.setLocation(250, 450);
                clearButton.setSize(100, 30);
                contentPane.add(clearButton);
                clearButton.addActionListener(this);
    
                // create the error label
                labelError = new JLabel();
                labelError.setText("Please correct items in red");
                labelError.setLocation(150, 500);
                labelError.setSize(190, 25);
                labelError.setForeground(Color.RED);
                labelError.setVisible(false);
                contentPane.add(labelError);
    
                // create the registration label
                labelRegistration = new JLabel();
                labelRegistration.setText("Thank you for your registration.");
                labelRegistration.setLocation(145, 500);
                labelRegistration.setSize(190, 25);
                labelRegistration.setForeground(Color.BLACK);
                labelRegistration.setVisible(false);
                contentPane.add(labelRegistration);
    
                // set properties of window
                setTitle("Registration Form");	//set window title
                setSize(475, 600); //set window size
                setVisible(true);
            } catch (Exception e) {
            }
        }// ends creatGUI method.
    
        public static void main(String args[]) {
            RegistrationForm application = new RegistrationForm();
            application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }// ends main
    
        public void actionPerformed(ActionEvent event) {
            try {
                if (event.getActionCommand().equals("Submit")) {
                    //Statements get executed here when SUBMIT button is pressed.
                    //Using the "&" instead of "&&" ensures that all check methods get called.
                    //If you used the "&", you would only see one error at a time.
                    //The form cannot be submitted until all the errors are fixed (every method returns true).
                    if (checkName() & checkAddress() & checkCity() & checkState() & checkZip()) {
                        labelRegistration.setVisible(true); //all information is entered and correct
                        labelError.setVisible(false);	    //hide this message if visible
                        submitButton.removeActionListener(this); // stop listening to submit button - form complete
                        clearButton.removeActionListener(this);  // stop listening to clear button - form complete
                    } else {
                        labelError.setVisible(true);   //all information is not valid on form
                        labelRegistration.setVisible(false); //hide this message when bad information entered
                    }
                } else {
                    textName.setText("");
                    textAddress.setText("");
                    textCity.setText("");
                    textState.setText("");
                    textZip.setText("");
                    labelError.setVisible(false);
                    labelRegistration.setVisible(false);
                    labelName.setForeground(Color.BLACK);
                    labelAddress.setForeground(Color.BLACK);
                    labelCity.setForeground(Color.BLACK);
                    labelState.setForeground(Color.BLACK);
                    labelZip.setForeground(Color.BLACK);
                }
            } catch (Exception e) { }
        }// ends actionPerformed method
    
        /* The checkName method looks at the contents of the name text field.
        The method requires that the length is greater than zero to pass (return true).
        If the length is greater than zero, the method returns true and sets the label
        color to black. If the length is zero, the method returns false and changes the
        label color to red.   */
        private boolean checkName() {
            if (textName.getText().length() == 0) {
                labelName.setForeground(Color.RED);	//name is not correct
                return false;
            } else {
                labelName.setForeground(Color.BLACK); //name is correct
                return true;
            }
        } //end of method
    
    
        /* The checkAddress method looks at the contents of the address text field.
        The method requires that the length is at least 5 to pass (return true).
        If the length is at least 5, the method returns true and sets the label
        color to black. If the length is less than 5, the method returns false and
        changes the label color to red.    */
        private boolean checkAddress() {
            if (textAddress.getText().length() < 5) {
                labelAddress.setForeground(Color.RED);	//address is not correct
                return false;
            } else {
                labelAddress.setForeground(Color.BLACK);	//address is correct
                return true;
            }
        } //end of method
    
        /* The checkCity method looks at the contents of the city text field.
        The method requires that the length is greater than zero to pass (return true).
        If the length is greater than zero, the method returns true and sets the label
        color to black. If the length is zero, the method returns false and changes the
        label color to red.   */
        private boolean checkCity() {
            if (textCity.getText().length() == 0) {
                labelCity.setForeground(Color.RED);	//city is not correct
                return false;
            } else {
                labelCity.setForeground(Color.BLACK);	//city is correct
                return true;
            }
        } //end of method
    
    
        /* The checkState method looks at the contents of the state text field.
        The method requires that the state be in the state array.  The for loop goes through every state in the state array
        checking to see if the state that the user entered was found (sets the isFound variable at true). If it finishes the loop
        and the isFound variable was never set to true, then that user's state was not found in the state array.  If the state was
        found, the method returns true and sets the label color to black. If the state was not found, the method returns false
        and changes the label color to red.  */
        private boolean checkState() {
            String stateArray[] = {"al", "ak", "az", "ar", "ca", "co", "de", "dc", "fl", "ga", "hi", "id", "il", "in",
                "ia", "ks", "ky", "la", "me", "md", "ma", "mi", "mn", "ms", "mo", "mt", "ne", "nv", "nh", "nj", "nm",
                "nc", "nd", "oh", "ok", "or", "pa", "ri", "sc", "sd", "tn", "tx", "ut", "vt", "va", "wa", "wv", "wi", "wy"};
    
            boolean isFound = false;
            for (int i = 0; i < stateArray.length; i++) {
                if (textState.getText().equalsIgnoreCase(stateArray[i])) {
                    isFound = true;
                }
            }
            if (isFound == true) {
                labelState.setForeground(Color.BLACK);	//state is correct
                return true;
            } else {
                labelState.setForeground(Color.RED);	//state is not correct
                return false;
            }
        } //end of method
    
    
        /* The checkZip method looks at the contents of the zip code field.
        The method requires that the zip code be an integer and be a length of 5 to pass.  If it passes it would return true
        and set the label color to black. If zip code isn't a number or is less/more than 5 characters long, the method
        would return false and set the label color to red. */
        private boolean checkZip() {
            try {
                Integer.parseInt(textZip.getText());        //zip is a number
    
                if (textZip.getText().length() == 5) {
                    labelZip.setForeground(Color.BLACK);	//zip is correct length
                    return true;
                } else {
                    labelZip.setForeground(Color.RED);	//zip is not correct length
                    return false;
                }
            } catch (Exception e) {
                labelZip.setForeground(Color.RED);          //zip is not a number
                return false;
            }
        } //end of method
    }// ends program
    Last edited by program junkie; 04-30-2011 at 11:37 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Thanks for editing your post! :)

    Now, for further help, you really should post your attempts to create the needed methods. You already have great examples of similar methods in the code above so it shouldn't be hard to at least get a start at this. Once you've done that, if you still have problems, please post your code.

  3. #3
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default

    well, the code was already written by the professor. My job is to adjust the actionperformed method.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by program junkie View Post
    well, the code was already written by the professor. My job is to adjust the actionperformed method.
    You've got two jobs actually:
    • Create three new methods, checkphone, checkemail, and checkpassword, that each checks the JTextField associated with it and returns a boolean depending on whether the JTextField's text is OK or not, and
    • Adjust actionPerformed so that it calls your three new methods. You only have to add them in the if boolean condition with the && operator along with the other method calls that are already there.


    So what are your thoughts on this?

  5. #5
    oomrichie is offline Member
    Join Date
    Feb 2011
    Posts
    31
    Rep Power
    0

    Default

    So what your saying is you haven't tried at all.:(

    First give it a go, and if your struggling I'll give you a hand

  6. #6
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default

    To be honest I don't know where to start with creating these three methods. I need a tutor if you are up to the task. thanks

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by program junkie View Post
    To be honest I don't know where to start with creating these three methods. I need a tutor if you are up to the task. thanks
    Tutoring is beyond the scope of this forum, but we can help you. As for what to do, please use the methods that the teacher has already given you as a model to build on. For e.g., here:

    Java Code:
        private boolean checkZip() {
            try {
                Integer.parseInt(textZip.getText());        //zip is a number
    
                if (textZip.getText().length() == 5) {
                    labelZip.setForeground(Color.BLACK);	//zip is correct length
                    return true;
                } else {
                    labelZip.setForeground(Color.RED);	//zip is not correct length
                    return false;
                }
            } catch (Exception e) {
                labelZip.setForeground(Color.RED);          //zip is not a number
                return false;
            }
        }


    what he does is create a method called checkZip that is declared to return a boolean, true if the zip code is OK, false if not.

    Java Code:
        private boolean checkZip() {

    Inside of this method, he gets the text held by the textZip field (all of your test methods will have an associated JTextField that you'll need to extract a String from using the getText() method) and tries to parse it into an int:

    Java Code:
            try {
                Integer.parseInt(textZip.getText());        //zip is a number

    He does this within a try/catch block because if it doesn't parse well, for instance if the user enters non-numeric data into the JTextField, the catch block will be called, and false will be returned.

    Here he checks if the zip's length is OK, and if so, the method returns true:

    Java Code:
                if (textZip.getText().length() == 5) {
                    labelZip.setForeground(Color.BLACK);	//zip is correct length
                    return true;

    If not, the method returns false:

    Java Code:
                } else {
                    labelZip.setForeground(Color.RED);	//zip is not correct length
                    return false;
                }

    Again this catch block is called if the String in the textZip does not represent a valid int:

    Java Code:
            } catch (Exception e) {
                labelZip.setForeground(Color.RED);          //zip is not a number
                return false;
            }
        }
    Again, at least try to give it a go with one of your methods. You've got nothing to lose by trying and all to gain.

  8. #8
    Zosa is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: Help with Action Performed method

    I have abit more of a problem because the checkPhone method needs to be added to program. This method should test that
    the user has entered the first 3 digits of the phone as 972 or 214. If the first 3 digits of
    phone number are 972 or 214, then the phone label foreground should be set to black and If
    the first 3 digits of the phone are NOT 972 and NOT 214, then the phone label foreground should be set to red. You should use the substring or startsWith method to do this. Do not use parenthesis when entering the phone number into the field. With the email this method should test that the user has entered a @ sign somewhere in the email field. If there is no @ in the email address, then the email label foreground should be set to red.
    The checkPassword method needs to be added to program. This method should check the text in the password textfield vs. the text in the retype password textfield. If both of these passwords are equal, then both labels foregrounds (password and retype password) should be set to black. If these passwords are not equal, then both labels foregrounds should be set to red. and this is what I have so far:

    import java.awt.*; //import older gui library for content pane
    import javax.swing.*; //import newer gui library for labels, textfields, and button
    import java.awt.event.*; //import gui event action library

    public class RegistrationForm extends JComponent implements ActionListener {

    JFrame frame = new JFrame("Registration Form");
    Container content = frame.getContentPane();
    // declare labels used on GUI screen
    private JLabel labelName, labelAddress, labelCity, labelState, labelZip, labelPhone, labelEmail, labelPassword,labelrePassword;
    private JLabel labelError, labelRegistration; // used for good and bad messages
    // declare text fields used on GUI screen
    private JTextField textName, textAddress, textCity, textState, textZip, textPhone, textEmail, textPassword,textrePassword;
    // declare button used on GUI screen
    private JButton submitButton, clearButton;

    public static void main(String args[]) {
    RegistrationForm application = new RegistrationForm();
    }

    public RegistrationForm() {
    try {
    content.add(this);

    // create the name label
    labelName = new JLabel(); //instantiate new JLabel
    labelName.setText("Name"); //set label text to name
    labelName.setLocation(38, 10); //set location of JLabel
    labelName.setSize(200, 25); //set size of JLabel
    labelName.setForeground(Color.BLACK);//set initial background color
    content.add(labelName); //add JLabel to content pane

    // create the name text box
    textName = new JTextField(); //instantiate new JTextField
    textName.setText(""); //clear JTextField
    textName.setToolTipText("Please type in full name - last name first");
    textName.setLocation(75, 10); //set location of JTextFfield
    textName.setSize(200, 25); //set size of JTextField
    content.add(textName); //add jextfield to content pane

    // create the address label
    labelAddress = new JLabel();
    labelAddress.setText("Address");
    labelAddress.setLocation(23, 50);
    labelAddress.setSize(80, 25);
    labelAddress.setForeground(Color.BLACK);
    content.add(labelAddress);

    // create the address text box
    textAddress = new JTextField();
    textAddress.setText("");
    textAddress.setToolTipText("Please type in full address");
    textAddress.setLocation(75, 50);
    textAddress.setSize(300, 25);
    content.add(textAddress);

    // create the city label
    labelCity = new JLabel();
    labelCity.setText("City");
    labelCity.setLocation(48, 90);
    labelCity.setSize(300, 25);
    labelCity.setForeground(Color.BLACK);
    content.add(labelCity);

    // create the city text box
    textCity = new JTextField();
    textCity.setText("");
    textCity.setToolTipText("Please type in full city");
    textCity.setLocation(75, 90);
    textCity.setSize(130, 25);
    content.add(textCity);

    // create the state label
    labelState = new JLabel();
    labelState.setText("State");
    labelState.setLocation(40, 130);
    labelState.setSize(300, 25);
    labelState.setForeground(Color.BLACK);
    content.add(labelState);

    // create the state text box
    textState = new JTextField();
    textState.setText("");
    textState.setToolTipText("Choose State");
    textState.setLocation(75, 130);
    textState.setSize(130, 25);
    content.add(textState);

    // create the zip label
    labelZip = new JLabel();
    labelZip.setText("Zip Code");
    labelZip.setLocation(23, 170);
    labelZip.setSize(300, 25);
    labelZip.setForeground(Color.BLACK);
    content.add(labelZip);

    // create the zip text box
    textZip = new JTextField();
    textZip.setText("");
    textZip.setToolTipText("Zip code must be between 10000 and 99999 inclusive");
    textZip.setLocation(75, 170);
    textZip.setSize(130, 25);
    content.add(textZip);

    // create the phone label
    labelPhone = new JLabel();
    labelPhone.setText("Phone Number");
    labelPhone.setLocation(23, 210);
    labelPhone.setSize(300, 25);
    labelPhone.setForeground(Color.BLACK);
    content.add(labelPhone);

    // create the phone text box
    textPhone = new JTextField();
    textPhone.setText("");
    textPhone.setToolTipText("Phone number must be 10 characters long");
    textPhone.setLocation(110, 210);
    textPhone.setSize(130, 25);
    content.add(textPhone);

    // create the email label
    labelEmail = new JLabel();
    labelEmail.setText("E-Mail");
    labelEmail.setLocation(23, 250);
    labelEmail.setSize(300, 25);
    labelEmail.setForeground(Color.BLACK);
    content.add(labelEmail);

    // create the email text box
    textEmail= new JTextField();
    textEmail.setText("");
    textEmail.setToolTipText("Please enter a valid email address");
    textEmail.setLocation(75, 250);
    textEmail.setSize(180, 25);
    content.add(textEmail);

    // create the password label
    labelPassword = new JLabel();
    labelPassword.setText("Password");
    labelPassword.setLocation(23, 290);
    labelPassword.setSize(300, 25);
    labelPassword.setForeground(Color.BLACK);
    content.add(labelPassword);

    // create the password text box
    textPassword= new JTextField();
    textPassword.setText("");
    textPassword.setToolTipText("Please enter a valid email address");
    textPassword.setLocation(90, 290);
    textPassword.setSize(230, 25);
    content.add(textPassword);

    // create the confirmation password label
    labelrePassword= new JLabel();
    labelrePassword.setText("Password re-enter");
    labelrePassword.setLocation(23, 330);
    labelrePassword.setSize(300, 25);
    labelrePassword.setForeground(Color.BLACK);
    content.add(labelrePassword);

    // create the confirmation password text box
    textrePassword= new JTextField();
    textrePassword.setText("");
    textrePassword.setToolTipText("Password must match in order to continue");
    textrePassword.setLocation(140, 330);
    textrePassword.setSize(230, 25);
    content.add(textrePassword);



    // create the submit button
    submitButton = new JButton();
    submitButton.setText("Submit");
    submitButton.setToolTipText("Click \"submit \" when the form is completely filled out");
    submitButton.setLocation(125, 450);
    submitButton.setSize(100, 30);
    content.add(submitButton);
    submitButton.addActionListener(this);

    clearButton = new JButton();
    clearButton.setText("Clear");
    clearButton.setToolTipText("Click \"clear \" when you want to clear the form");
    clearButton.setLocation(250, 450);
    clearButton.setSize(100, 30);
    content.add(clearButton);
    clearButton.addActionListener(this);

    // create the registration label
    labelRegistration = new JLabel();
    labelRegistration.setText("Thank you for your registration.");
    labelRegistration.setLocation(145, 500);
    labelRegistration.setSize(190, 25);
    labelRegistration.setForeground(Color.BLACK);
    labelRegistration.setVisible(false);
    content.add(labelRegistration);

    // create the error label
    labelError = new JLabel();
    labelError.setText("Please correct items in red");
    labelError.setLocation(150, 500);
    labelError.setSize(190, 25);
    labelError.setForeground(Color.RED);
    labelError.setVisible(false);
    content.add(labelError);

    // set properties of window
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setSize(475, 600);
    frame.setLayout(null);
    frame.setVisible(true);
    } catch (Exception e) {
    }
    }

    public void actionPerformed(ActionEvent event) {
    try {
    if (event.getActionCommand().equals("Submit")) {
    //Statements get executed here when SUBMIT button is pressed.
    //Using the "&" instead of "&&" ensures that all check methods get called.
    //If you used the "&&", you would only see one error at a time.
    //The form cannot be submitted until all the errors are fixed (every method returns true).
    if (checkName() & checkAddress() & checkCity() & checkState() & checkZip()& checkPhone()& checkrePassword()& checkEmail()& checkPassword()) {
    labelRegistration.setVisible(true); //all information is entered and correct
    labelError.setVisible(false); //hide this message if visible
    } else {
    labelError.setVisible(true); //all information is not valid on form
    labelRegistration.setVisible(false); //hide this message when bad information entered
    }
    } else {
    textName.setText("");
    textAddress.setText("");
    textCity.setText("");
    textState.setText("");
    textZip.setText("");
    textPhone.setText("");
    textEmail.setText("");
    textPassword.setText("");
    textrePassword.setText("");
    labelError.setVisible(false);
    labelRegistration.setVisible(false);
    labelName.setForeground(Color.BLACK);
    labelAddress.setForeground(Color.BLACK);
    labelCity.setForeground(Color.BLACK);
    labelState.setForeground(Color.BLACK);
    labelZip.setForeground(Color.BLACK);
    labelPhone.setForeground(Color.BLACK);
    labelEmail.setForeground(Color.BLACK);
    labelPassword.setForeground(Color.BLACK);
    labelrePassword.setForeground(Color.BLACK);
    }
    } catch (Exception e) {
    }
    }

    /* The checkName method looks at the contents of the name text field.
    The method requires that the length is greater than zero to pass (return true).
    If the length is greater than zero, the method returns true and sets the label
    color to black. If the length is zero, the method returns false and changes the
    label color to red. */
    private boolean checkName() {
    if (textName.getText().length() == 0) {
    labelName.setForeground(Color.RED); //name is not correct
    return false;
    } else {
    labelName.setForeground(Color.BLACK); //name is correct
    return true;
    }
    }


    /* The checkAddress method looks at the contents of the address text field.
    The method requires that the length is at least 5 to pass (return true).
    If the length is at least 5, the method returns true and sets the label
    color to black. If the length is less than 5, the method returns false and
    changes the label color to red. */
    private boolean checkAddress() {
    if (textAddress.getText().length() < 5) {
    labelAddress.setForeground(Color.RED); //address is not correct
    return false;
    } else {
    labelAddress.setForeground(Color.BLACK); //address is correct
    return true;
    }
    }

    /* The checkCity method looks at the contents of the city text field.
    The method requires that the length is greater than zero to pass (return true).
    If the length is greater than zero, the method returns true and sets the label
    color to black. If the length is zero, the method returns false and changes the
    label color to red. */
    private boolean checkCity() {
    if (textCity.getText().length() == 0) {
    labelCity.setForeground(Color.RED); //city is not correct
    return false;
    } else {
    labelCity.setForeground(Color.BLACK); //city is correct
    return true;
    }
    }


    /* The checkState method looks at the contents of the state text field.
    The method requires that the state be in the state array. The for loop goes through every state in the state array
    checking to see if the state that the user entered was found (sets the isFound variable at true). If it finishes the loop
    and the isFound variable was never set to true, then that user's state was not found in the state array. If the state was
    found, the method returns true and sets the label color to black. If the state was not found, the method returns false
    and changes the label color to red. */
    private boolean checkState() {
    String stateArray[] = {"al", "ak", "az", "ar", "ca", "co", "ct", "de", "dc", "fl", "ga", "hi", "id", "il", "in",
    "ia", "ks", "ky", "la", "me", "md", "ma", "mi", "mn", "ms", "mo", "mt", "ne", "nv", "nh", "nj", "nm",
    "nc", "nd", "oh", "ok", "or", "pa", "ri", "sc", "sd", "tn", "tx", "ut", "vt", "va", "wa", "wv", "wi", "wy"};

    boolean isFound = false;
    for (int i = 0; i < stateArray.length; i++) {
    if (textState.getText().equalsIgnoreCase(stateArray[i])) {
    isFound = true;
    }
    }
    if (isFound == true) {
    labelState.setForeground(Color.BLACK); //state is correct
    return true;
    } else {
    labelState.setForeground(Color.RED); //state is not correct
    return false;
    }
    }


    /* The checkZip method looks at the contents of the zip code field.
    The method requires that the zip code be an integer and be a length of 5 to pass. If it passes it would return true
    and set the label color to black. If zip code isn't a number or is less/more than 5 characters long, the method
    would return false and set the label color to red. */
    private boolean checkZip() {
    try {
    Integer.parseInt(textZip.getText()); //zip is a number

    if (textZip.getText().length() == 5) {
    labelZip.setForeground(Color.BLACK); //zip is correct length
    return true;
    } else {
    labelZip.setForeground(Color.RED); //zip is not correct length
    return false;
    }
    } catch (Exception e) {
    labelZip.setForeground(Color.RED); //zip is not a number
    return false;
    }
    }

    //////////////////////////////////////////////////////my code

    private boolean checkPhone ()
    {
    if ((textPhone.getText().substring(0,4).equals("972") ) && (textPhone.getText().substring(0,4).equals("214")) )
    {
    labelPhone.setForeground(Color.BLACK); //phone is correct
    return true;
    }
    else
    {
    labelPhone.setForeground(Color.RED); //phone is incorrect
    return false;
    }
    }//end of method

    private boolean checkEmail() {
    try {
    Integer.parseInt(textEmail.getText()); //e is a number

    if (textEmail.getText().length() == 10) {
    labelZip.setForeground(Color.BLACK); //e is correct length
    return true;
    } else {
    labelEmail.setForeground(Color.RED); //e is not correct length
    return false;
    }
    } catch (Exception e) {
    labelEmail.setForeground(Color.RED); //e is not a number
    return false;
    }
    }

    private boolean checkPassword() {
    try {
    Integer.parseInt(textPassword.getText()); //password is a number

    if (textPassword.getText().length() == 10) {
    labelZip.setForeground(Color.BLACK); //password is correct length
    return true;
    } else {
    labelPassword.setForeground(Color.RED); //password is not correct length
    return false;
    }
    } catch (Exception e) {
    labelPassword.setForeground(Color.RED); //password is not a number
    return false;
    }
    }

    private boolean checkrePassword() {
    try {
    Integer.parseInt(textrePassword.getText()); //zip is a number

    if (textrePassword.getText().length() == 10) {
    labelZip.setForeground(Color.BLACK); //zip is correct length
    return true;
    } else {
    labelrePassword.setForeground(Color.RED); //zip is not correct length
    return false;
    }
    } catch (Exception e) {
    labelrePassword.setForeground(Color.RED); //zip is not a number
    return false;
    }
    }


    }

    Again, I can't figure out the phone to the password booleans.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with Action Performed method

    Please don't hijack someone else's thread for a new problem. If you have your own question, please post a new thread, and read the forum FAQ to learn how to use code tags so that your code is readable. Locking.

Similar Threads

  1. action performed method for
    By thousif291989 in forum AWT / Swing
    Replies: 3
    Last Post: 04-21-2011, 06:08 PM
  2. Replies: 2
    Last Post: 03-31-2011, 12:10 AM
  3. Replies: 2
    Last Post: 03-23-2009, 10:02 AM
  4. Replies: 3
    Last Post: 08-08-2008, 03:53 AM
  5. JXTaskPane Action performed
    By Rama Koti Reddy in forum AWT / Swing
    Replies: 0
    Last Post: 12-10-2007, 02:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •