Java - Check if JTextField is empty or not

前端 未结 11 945
北恋
北恋 2020-11-27 17:44

So I got know this is a popular question and already found the solution. But when I try this it doesn\'t work properly.

My JTextField is empty and the button isn\'t

相关标签:
11条回答
  • 2020-11-27 17:55

    you can use isEmpty() or isBlank() methods regarding what you need.


    Returns true if, and only if, length() is 0.

    this.name.getText().isEmpty();
    

    Returns true if the string is empty or contains only white space codepoints, otherwise false

    this.name.getText().isBlank();
    
    0 讨论(0)
  • 2020-11-27 18:00

    The following will return true if the JTextField "name" does not contain text:

    name.getText().isEmpty

    0 讨论(0)
  • 2020-11-27 18:03

    use the following code :

    if(name.getText().equals(""))
    {
    loginbt.disable();
    }
    
    0 讨论(0)
  • 2020-11-27 18:04

    To Check JTextFiled is empty or not condition:

    if( (billnotf.getText().length()==0)||(billtabtf.getText().length()==0))
    
    0 讨论(0)
  • 2020-11-27 18:04

    Try this

    if(name.getText() != null && name.getText().equals(""))
    {
            loginbt.setEnabled(false);
    }
    else
    {
            loginbt.setEnabled(true);
    }
    
    0 讨论(0)
  • 2020-11-27 18:05
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // Submit Button        
    
        String Fname = jTextField1.getText();
        String Lname = jTextField2.getText();
        String Desig = jTextField3.getText();
        String Nic = jTextField4.getText();
        String Phone = jTextField5.getText();
        String Add = jTextArea1.getText();
        String Dob = jTextField6.getText();
        // String Gender;
        // Image
    
        if (Fname.hashCode() == 0 || Lname.hashCode() == 0 || Desig.hashCode() == 0 || Nic.hashCode() == 0 || Phone.hashCode() == 0 || Add.hashCode() == 0)
        {
            JOptionPane.showMessageDialog(null, "Some fields are empty!");
        }
        else
        {
            JOptionPane.showMessageDialog(null, "OK");
        }
    }
    
    0 讨论(0)
提交回复
热议问题