So the problem that I am having is I am trying to have an account creation area of an app and whilst doing this they have to enter the password for the account twice. If the passwords are the same the account will be created otherwise they are told that they are different and asked to try again.
The problem that I am having is that despite the data in the two JTextFields being the same when they are compared using an == they are apparently different. The code that I have tried is shown below.
JLabel PasswordL = new JLabel ();
PasswordL.setText("Password:");
UserPanel.add(PasswordL,"cell 1 0,width 150,height 25,span 2 1");
final JTextField PasswordT = new JTextField ();
UserPanel.add(PasswordT,"cell 1 1,width 150,height 25");
JLabel ConfirmPasswordL = new JLabel ();
ConfirmPasswordL.setText("Confirm Password:");
UserPanel.add(ConfirmPasswordL,"cell 1 2,width 150,height 25,span 2 1");
final JTextField ConfirmPasswordT = new JTextField ();
UserPanel.add(ConfirmPasswordT,"cell 1 3,width 150,height 25");
JButton AddNewUser = new JButton ();
AddNewUser.setText("Insert");
AddUserButtonPanel.add(AddNewUser,"cell 0 0,width 100,height 25");
AddNewUser.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent e) {
System.out.println(new String(PasswordT.getText()));
System.out.println(new String(ConfirmPasswordT.getText()));
if (new String(PasswordT.getText()) == new String(ConfirmPasswordT.getText())) {
AdminConsole AC = new AdminConsole ();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
new ErrorWindow("This account already exists!");
}
} else {
JFrame Error = new JFrame();
Error.add(new JLabel("The Password do not match!"));
Error.setSize(200, 50);
Error.setLocationRelativeTo(null);
Error.setVisible(true);
}
}
});
I have tried both with and without the use of new String () and now I have run out of ideas. Any advice would be greatly appreciated thanks.