Java if statement does not apply

I have a perfectly working app that is coded in java. But one if statement does not want to work... :(

login.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            String usernamed = nameField.getText();
            String passwordd = String.valueOf(passwortField.getPassword());


            if (usernamed == "user") {
                if (passwordd == "pass") {

                    JFrame tickets = new tickets(usernamed);
                    tickets.setSize(1000, 700);
                    tickets.setTitle("Tickets - Start");
            } else {
                error.setText("Wrong Password/ Username");


When i write "user" in the JTextField nameField, "pass" into the JPasswordField passwortField and click on the JButton login, It should open a new Window but it doesn`t and JLabel error gets set to: "Wrong Password/ Username". But in the Console i get the Outprint:


What am i doing wrong?

1 answer

  • answered 2017-11-12 20:13 Damian Lattenero

    Use equals here my friend,

    password.equals( "pass")

    And here


    And in all the places you are comparing strings with ==

    Strings in Java are objects, if you use == you are checking the reference to the object, not the string inside