Results 1 to 5 of 5
  1. #1
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default i managed to fix the last problem, but another problem came along. [help]

    so this program asks you to fill in a username, a password, and a confirmation of the password.
    what i want to do is: if you enter the password and password confirmation both the same (example: password: hey, confirmation: hey) then i want to make a window pop up with the account details (username and password), but if you dont enter the password fields in the same (example: password: hey, confirmation: no) then i want to make a window pop up with and error message telling you to try again.

    i tried, but the problem is: even when both password fields are filled in the same, it displays the error message.
    ps1 and ps2 are the names of the password fields.

    Java Code:
    public class thehandler implements ActionListener{
    	public void actionPerformed(ActionEvent event){
    		//if the passwords are equal to eachother (the confirmation and the password itself) then show a window with this below
    		if(ps1.getText() == ps2.getText())
    		JOptionPane.showMessageDialog(null, "Account information: "+"\n"+"User: "+txt1.getText()+"\n"+"Password: "+ps1.getText(), "Account Information", JOptionPane.INFORMATION_MESSAGE);
            //if the entered passwords dont match eachother, then show a window with this below
    		else
    		JOptionPane.showMessageDialog(null, "Passwords did not match! try again please!", "Password error!", JOptionPane.PLAIN_MESSAGE);
    		//the problem is, even when the passwords match eachother, it disaplays the above error message! :o
    			
    		
    	}
    }
    }
    the whole code:

    Java Code:
    import javax.swing.JOptionPane;
    import java.awt.event.ActionListener;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import javax.swing.JLabel;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JTextField;
    import javax.swing.JPasswordField;
    public class LogIn extends JFrame{
    	
    	private JTextField txt1;
    	private JPasswordField ps1;
    	private JPasswordField ps2;
    	private JLabel label1;
    	private JLabel label2;
    	private JLabel label3;
    	private JButton b1;
     
    	
    	
    
    
    public LogIn(){
    	super("Account");
    	setLayout (new FlowLayout());
    	label1 = new JLabel("Enter your username: ");
    	txt1 = new JTextField(10);
    	label2 = new JLabel("Enter your password: ");
    	ps1 = new JPasswordField(10);
    	label3 = new JLabel("Confirm your password: ");
    	ps2 = new JPasswordField(10);
    	b1 = new JButton("OK");
    	add(label1);
    	add(txt1);
    	add(label2);
    	add(ps1);
    	add(label3);
    	add(ps2);
    	add(b1);
    	
    	
    	thehandler handler = new thehandler();
    	b1.addActionListener(handler);
    	
    	
    }
    public class thehandler implements ActionListener{
    	public void actionPerformed(ActionEvent event){
    		//if the passwords are equal to eachother (the confirmation and the password itself) then show a window with this below
    		if(ps1.getText() == ps2.getText())
    		JOptionPane.showMessageDialog(null, "Account information: "+"\n"+"User: "+txt1.getText()+"\n"+"Password: "+ps1.getText(), "Account Information", JOptionPane.INFORMATION_MESSAGE);
            //if the entered passwords dont match eachother, then show a window with this below
    		else
    		JOptionPane.showMessageDialog(null, "Passwords did not match! try again please!", "Password error!", JOptionPane.PLAIN_MESSAGE);
    		//the problem is, even when the passwords match eachother, it disaplays the above error message! :o
    			
    		
    	}
    }
    }

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: i managed to fix the last problem, but another problem came along. [help]

    Also get text is not longer in use for JPasswords, use getPassword() instead


    you cant use == to compare strings.

    change the == String is not a primitive type. change the == to .equals:

    if(ps1.getPassword().equals(ps2.getPassword())) that should be it.

  3. #3
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default Re: i managed to fix the last problem, but another problem came along. [help]

    thank a LOT bro, really.

  4. #4
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: i managed to fix the last problem, but another problem came along. [help]

    but since getPassword returns an array on chars, you may need to adjust the way you compare them

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

    Default Re: i managed to fix the last problem, but another problem came along. [help]

    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Problem with session-scoped managed beans in JSF
    By daver690 in forum Hibernate
    Replies: 3
    Last Post: 05-14-2012, 11:56 AM
  2. datatable/ managed bean
    By maitoti in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 04-18-2011, 05:49 PM
  3. JSF: Managed-Bean with Reuqest Scope
    By Denton in forum JavaServer Faces (JSF)
    Replies: 2
    Last Post: 03-11-2011, 08:21 AM
  4. EntityManager.refresh() works only on managed entities.
    By CatchSandeepVaid in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 12-05-2009, 12:11 PM
  5. Container Managed Persistence
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-27-2007, 10:16 AM

Posting Permissions

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