javaswingvalidationif-statementfocuslistener

If else statement is giving wrong output


Im trying to add validation on my textbox in my code by using if else condition

Ive added focusListener on the textbox that will check the condition when I remove the focus from textbox
Ive also created a label beside the chekbox which will display text according to the condition

If the text inside the textbox is not equal to "hi" then it should print "hello" in the label beside textbox

otherwise it should print "bye"

import java.awt.*;  
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.*;

public class regframe extends JFrame implements ActionListener
{
    
    private static final long serialVersionUID = 1L;
    JTextField t1,t2;
    JLabel l1,l2,l3,l4;
    JButton b;

    regframe()
    {   
        getContentPane().setBackground(Color.BLACK);
        l1=new JLabel("Enter First Num:");
        l1.setBounds(50,50,150,20);
        l1.setForeground(Color.WHITE);
        l1.setFont(new Font("Verdana",Font.BOLD,12));
        add(l1);

        t1=new JTextField();
        t1.setBounds(180,50,100,20);
        t1.setBackground(Color.BLACK);
        t1.setForeground(Color.WHITE);
        t1.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0,Color.YELLOW));
        t1.setCaretColor(Color.CYAN);
        t1.setFont(new Font("Verdana",Font.BOLD,12));
        t1.addActionListener(this);
        add(t1);
        
        l3=new JLabel();
        l3.setBounds(290,50,150,20);
        l3.setForeground(Color.CYAN);
        l3.setOpaque(false);
        l3.setFont(new Font("Verdana",Font.BOLD,10));
        add(l3);

        t1.addFocusListener(new FocusAdapter() 
        {
            public void focusLost(FocusEvent e)
            {
                if(t1.getText()!="hi")
                {
                    l3.setText("hello");
                }
                else
                {
                    l3.setText("bye");
                }
            }
        });
        

        l2=new JLabel("Enter Second Num:");
        l2.setBounds(50,80,150,20);
        l2.setForeground(Color.WHITE);
        l2.setFont(new Font("Verdana",Font.BOLD,12));
        add(l2);        

        t2=new JTextField();
        t2.setBounds(180,80,100,20);
        t2.setBackground(Color.BLACK);
        t2.setForeground(Color.WHITE);
        t2.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0,Color.YELLOW));
        t2.setCaretColor(Color.CYAN);
        t2.setFont(new Font("Verdana",Font.BOLD,12));
        add(t2);
                
        b=new JButton("Submit");
        b.setBounds(100,120,100,20);
        b.setBackground(Color.CYAN);
            b.setForeground(Color.BLACK);
        add(b);

        setLocation(300,200);
        setSize(700,250);
        setLayout(null);
        setTitle("Registration");
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        
    }
    
    public static void main(String args[])
    {
        new regframe();
    }

    public void actionPerformed(ActionEvent arg0) 
    {
        
        
    }
}

But when I run the code, It always display "hello" even when I write "hi" inside the textbox enter image description here

enter image description here

Please Help


Solution

  • use

    if(!"hi".equals(t1.getText()) 
    

    instead of

    if(t1.getText()!="hi")
    

    You could also do if(!t1.getText().equals("hi")) but the other method is more null-safe

    Take a look at https://www.javatpoint.com/string-comparison-in-java