Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use if else to set var value in TextField

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!");
        }
like image 347
AJ AJ Avatar asked Dec 08 '25 18:12

AJ AJ


2 Answers

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?

like image 127
Supun Amarasinghe Avatar answered Dec 10 '25 12:12

Supun Amarasinghe


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") && ...
like image 23
ΦXocę 웃 Пepeúpa ツ Avatar answered Dec 10 '25 10:12

ΦXocę 웃 Пepeúpa ツ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!