Why didn't my GUI login set the warning label correctly?

I am writing a simple user log on the screen that will store users through serialization. The GUI is beautiful, and all that’s really left is to implement serialization, but I can’t get my warningLabel display the text correctly when the user enters the wrong password or username. It will display the first error message, but if another error occurs, the label remains the same. I need a label to change EVERY TIME, there is an error. I will send all the code below.

UserCreateAccountGUI Class:

 package userInfoAndSerialization; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Arrays; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.UIManager; public class UserCreateAccount implements ActionListener { public static int numOfUsers; String username; String password; public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } UserCreateAccount ucaGUI = new UserCreateAccount(); ucaGUI.start(); } JFrame frame; JPanel panel; JTextField usernameField; JPasswordField passwordField; JPasswordField confirmPasswordField; JLabel warningLabel; public void start() { frame = new JFrame("Create a new account"); panel = new JPanel(); panel.setLayout(new GridBagLayout()); frame.getContentPane().add(BorderLayout.CENTER, panel); panel.setBackground(Color.ORANGE); JLabel userLabel = new JLabel("Username:"); JLabel passwordLabel = new JLabel("Password:"); JLabel confirmPasswordLabel = new JLabel("Confirm Password:"); usernameField = new JTextField(15); passwordField = new JPasswordField(15); confirmPasswordField = new JPasswordField(15); GridBagConstraints right = new GridBagConstraints(); right.anchor = GridBagConstraints.WEST; GridBagConstraints left = new GridBagConstraints(); left.anchor = GridBagConstraints.EAST; right.weightx = (int) 2; right.fill = GridBagConstraints.REMAINDER; right.gridwidth = GridBagConstraints.REMAINDER; // actual GUI panel.add(userLabel, left); panel.add(usernameField, right); panel.add(passwordLabel, left); panel.add(passwordField, right); panel.add(confirmPasswordLabel, left); panel.add(confirmPasswordField, right); frame.setSize(300, 250); frame.setVisible(true); JButton createAccount = new JButton("Create this account"); frame.getContentPane().add(BorderLayout.SOUTH, createAccount); createAccount.addActionListener(this); warningLabel = new JLabel(); frame.getContentPane().add(BorderLayout.NORTH, warningLabel); } // this is where the problem is. public void actionPerformed(ActionEvent event) { if (!(passwordField.getPassword().toString().equals(confirmPasswordField.getPassword().toString()))) { warningLabel.setText("Your passwords do not match! Please try again!"); } else if (passwordField.getPassword().toString().length() < 1 ) { warningLabel.setText("Your password is not long enough! Please try again!"); } else if (usernameField.getText().length() < 1) { warningLabel.setText("Your username is not long enough! Please try again!"); } else { warningLabel.setText("Account created successfully."); } } } 
+4
source share
1 answer

This will not fly:

 passwordField.getPassword().toString(). equals(confirmPasswordField.getPassword().toString()) 

It is better to print the results of the .toString() call in a char array to see exactly what I mean.

For example, when I run:

 String fooString = "Foo"; char[] fooArray = fooString.toCharArray(); System.out.println(fooArray.toString()); 

It does not return "Foo" as you seem to expect, but rather a typical and expected representation of the toString() the char array: [ C@19821f . Please note: if you run this, your hash code number will be different from mine (the same if I run it a second time!).

It is better to use the method of the Arrays equals(...) class so that you can compare two char arrays. i.e.,

 char[] pw1 = passwordField.getPassword(); char[] pw2 = confirmPasswordField.getPassword(); if (Arrays.equals(pw1, pw2)) { //... } 

Note. A bad solution would be to convert the char arrays to a β€œreal” string using new String(myCharArray) , but I highly recommend not to do this, as your passwords are very weak and easily broken.

+2
source

All Articles