3

can I ask how I can instantly equal my TextField named txtUserName into 'Aime' and also my txtPassword equal to 'Joy' so that it will show the message "User Name and Password Match!" ? Anyone? Please help me :(

public void actionPerformed(ActionEvent e){
    if (e.getSource()== btnClear){
    txtUserName.setText("");
    }
    if(e.getSource() == btnLogin){
        if (txtUserName.setText="Aime" && txtPassword.setText="JOy")){
            JOptionPane.showMessageDialog(null, "User Name and Password Match!");
        }
        else {
            JOptionPane.showMessageDialog(null, "User Name and Password Invalid!");
        }
AJ AJ
  • 27
  • 3
  • 1
    can u describe your requirement? – user3808887 Sep 12 '17 at 09:27
  • does this code even compile?? – ΦXocę 웃 Пepeúpa ツ Sep 12 '17 at 09:30
  • Hello it is a Log-in Form , My professor ask me to insert an username in textfield1 and also a password in textfield2 and if they match on the required username and password the text "User Name and Password Match!" will print through JOptionPane and if it is not it would print "User Name and Password Invalid!" – AJ AJ Sep 12 '17 at 09:30
  • `txtUserName.setText="Aime"` is completely wrong here. First, setText is a function, not a field. Second, if you want to compare 2 two `String` (or 2 `Objects`), `equals` is always referred. – cuongnguyen Sep 12 '17 at 09:33

2 Answers2

4

These are the Small modifications needed.

if (txtUserName.getText().equals("Aime") && txtPassword.getText().equals("Joy")){
            JOptionPane.showMessageDialog(null, "User Name and Password Match!");
}else{
        JOptionPane.showMessageDialog(null, "User Name and Password Invalid!");
}

For your reference : What is the difference between == vs equals() in Java?

Supun Amarasinghe
  • 1,443
  • 3
  • 12
  • 24
2

you have two problems here:

txtUserName.setText="Aime" && txtPassword.setText="JOy"

am guessing you are trying to check the user input so you need to use the gettext method...

on the other hand you can not compare strings using ==, but furthermore you mistyped the == and are instead of comparing assigning, assignment that is invalid since you can not set the text of that view in that way...

try instead

txtUserName.getText().toString().equals("Aime") && ...
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97