0

I was doing some programming one night and I ran into a problem. It seems the if statement in the action listener for my button is not being picked up. I am farely new to programming so I hope it is not anything too obvious Any suggestions? Here's the code:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.util.*;
import javax.swing.*;

public class Login extends JFrame implements ActionListener {
JTextField tfUsername, tfPassword;
JLabel jlUser, jlPass;
JTextArea tarea;
JButton bLogin;

public Login(){
    JFrame frame = new JFrame("Login Test");
    JPanel panel = new JPanel();
    jlUser = new JLabel("Username");
    tfUsername = new JTextField(20);
    jlPass = new JLabel("Password");
    tfPassword = new JTextField(20);
    bLogin = new JButton("Login");
    tarea = new JTextArea();

    frame.setSize(335,150);
    frame.add(panel);
    panel.add(jlUser);
    panel.add(tfUsername);
    panel.add(jlPass);
    panel.add(tfPassword);
    panel.add(bLogin);
    panel.add(tarea);
    bLogin.addActionListener(this);
    frame.setResizable(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}


public static void main(String[] args) {
new Login();

}


@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    if(e.getSource() == bLogin){
        String user = tfUsername.getText();
        String pass = tfPassword.getText();
        String be = "hi";

                    if(pass == be){
            tarea.setText("Successful");
        }
}

}

2 Answers 2

6

The problem is with how you are comparing the pass and be Strings

if (pass == be) {
    tarea.setText("Successful");
}

This is comparing the Object references, not their content, which will always be false...Instead you should be using .equals

if (pass.equals(be)) {
    tarea.setText("Successful");
}

This will do a case sensitive comparison. If required, you can also use .equalsIgnoreCase where the case does not matter

For passwords, you should be using JPasswordField and storing the password in a char array, as String values are easier to pick out of memory.

This would then require you to use Arrays.equals(char\[\], char\[\]) instead

See How to use Password Fields for more details...

Sign up to request clarification or add additional context in comments.

Comments

1

So far what i can see is the String compare error. Post the error log to see if there is any other error. In the string match try

if (pass.equals(be)) {
    tarea.setText("Successful");
}

You can also use compareto function as well to check String

Also gettext for pass is not very secured

3 Comments

u are faster, congrats
Sorry, it's early and I've had little sleep, so I'm a little cynical. I'm certainly not against people expanding on answers or providing additional points of view, but it does get annoying when people populate the questions with the same answer - IMHO :P
Apologies, I'm having a rant, and you just got in the way :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.