Results 1 to 3 of 3
  1. #1
    juniorwally is offline Member
    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Default How can i fix the Validation for this Class?

    I'm trying to fix the Validation for my UserWindow class, it works fine, if all inputs are incorrect and only works if the name field is incorrect. However, all other fields still process to the next window, specifically the last two fields. How can I fix that?

    Java Code:
    import javax.swing.*;
    
    import java.awt.GridLayout;
    import java.awt.HeadlessException;
    import java.awt.event.*;
    
    //import statements here
    public class UserWindow extends JFrame {
    
    	private JTextField nameField, ageField, creditCardField;
    	private JButton backButton, nextButton;
    
    	public UserWindow() {
    		super("Please enter your information");
    
    		setSize(700, 400);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setLocationRelativeTo(null);
    		setLayout(new GridLayout(4, 2));
    
    		buildPanel();
    		setVisible(true);
    
    	}
    
    	private void buildPanel() {
    
    		nameField = new JTextField(10);
    		ageField = new JTextField(2);
    		creditCardField = new JTextField(10);
    
    		backButton = new JButton("Back");
    		nextButton = new JButton("Next");
    		nextButton.addActionListener(new NextButton());
    		backButton.addActionListener(new BackButton());
    
    		JLabel NameLabel = new JLabel("Please enter your name");
    
    		JLabel ageLabel = new JLabel("Enter your age");
    
    		JLabel creditCardLabel = new JLabel("Enter your credit card number");
    
    		JPanel panel1 = new JPanel();
    		JPanel panel2 = new JPanel();
    		JPanel panel3 = new JPanel();
    		JPanel panel4 = new JPanel();
    		JPanel panel5 = new JPanel();
    		JPanel panel6 = new JPanel();
    		JPanel panel7 = new JPanel();
    		JPanel panel8 = new JPanel();
    
    		add(panel1);
    		add(panel2);
    		add(panel3);
    		add(panel4);
    		add(panel5);
    		add(panel6);
    		add(panel7);
    		add(panel8);
    
    		panel1.add(NameLabel);
    		panel2.add(nameField);
    		panel4.add(ageField);
    		panel6.add(creditCardField);
    		panel3.add(ageLabel);
    		panel5.add(creditCardLabel);
    		panel7.add(backButton);
    		panel8.add(nextButton);
    
    	}// end of panel building
    
    	// action listeners for fields/buttons
    
    	private class NextButton implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			String str = nameField.getText();
    			
    			int age;
    			
    			int ccNum;
    
    			if (str.equals("")) { // User have not entered anything.
    				JOptionPane.showMessageDialog(null, "Please enter your name.");
    				nameField.requestFocusInWindow();
    				// Do NOT loop here.
    			} else {
    				// Do everything you need to do when the user have entered
    				// something
    				new MovieSelection();
    				setVisible(false);
    
    			}
    
    			try {
    				age = Integer.parseInt(ageField.getText());
    
    			} catch (Exception z) {
    
    				JOptionPane.showMessageDialog(null, "Nope", "Inane error",
    						JOptionPane.OK_OPTION);
    				ageField.requestFocusInWindow();
    
    			}
    
    			 
    			try{
    				ccNum = Integer.parseInt(creditCardField.getText());
    				
    			}catch (Exception y){
    				JOptionPane.showMessageDialog(null, "Nope", "Inane error",
    						JOptionPane.OK_OPTION);
    				ageField.requestFocusInWindow();
    			}
    			
    			//end of validation
    
    		}
    	}
    
    	private class BackButton implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			// return back to home page
    			if (e.getSource() == backButton)
    				setVisible(false);
    				new SelectUserWindow();
    		}
    	}
    
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,010
    Rep Power
    20

    Default Re: How can i fix the Validation for this Class?

    Look at your if/else check for the name.
    This is your first check, and if the 'str' is not blank then you open the next screen.
    It never gets to the other checks.

    I would suggest either using a flag, then at the end if 'valid' then open the next screen, or nest your checks, so if the 'str' is valid, then check the 'age', if that's valid then check the ccNum, and if that is valid then move onto the next screen.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    juniorwally is offline Member
    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Default Re: How can i fix the Validation for this Class?

    Quote Originally Posted by Tolls View Post
    Look at your if/else check for the name.
    This is your first check, and if the 'str' is not blank then you open the next screen.
    It never gets to the other checks.

    I would suggest either using a flag, then at the end if 'valid' then open the next screen, or nest your checks, so if the 'str' is valid, then check the 'age', if that's valid then check the ccNum, and if that is valid then move onto the next screen.
    Thanks, your suggestion helped!

Similar Threads

  1. Validation
    By Johnny2009 in forum New To Java
    Replies: 2
    Last Post: 09-23-2012, 11:11 PM
  2. Validation of XML against XSD
    By abhinav_sharma in forum New To Java
    Replies: 0
    Last Post: 05-02-2012, 09:18 AM
  3. XML validation
    By Onra in forum New To Java
    Replies: 0
    Last Post: 03-24-2011, 06:14 PM
  4. XML Validation
    By sehudson in forum XML
    Replies: 5
    Last Post: 03-21-2011, 12:38 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
  •