[java] Java - Check if JTextField is empty or not

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 enabled. When I write something in my textfield the button doesn't get enabled.

So my program should check this field every second whether it's empty or not. As soon as someone writes something into the textfield the button should be enabled.^^

loginbt = new JButton("Login");
    loginbt.addActionListener(new loginHandler());
    add(loginbt);

    if(name.getText().equals("")) {
        loginbt.setEnabled(false);
    }else {
        loginbt.setEnabled(true);
    }

This question is related to java swing jbutton jtextfield documentlistener

The answer is


Try with keyListener in your textfield

jTextField.addKeyListener(new KeyListener() {

        @Override
        public void keyTyped(KeyEvent e) {
        }

        @Override
        public void keyPressed(KeyEvent e) {
            if (text.getText().length() >= 1) {
                button.setEnabled(true);
            } else {
                button.setEnabled(false);
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {
        }

    });

use the following code :

if(name.getText().equals(""))
{
loginbt.disable();
}

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

name.getText().isEmpty


if(name.getText().hashCode() != 0){
    JOptionPane.showMessageDialog(null, "not empty");
}
else{
    JOptionPane.showMessageDialog(null, "empty");
}

Try this

if(name.getText() != null && name.getText().equals(""))
{
        loginbt.setEnabled(false);
}
else
{
        loginbt.setEnabled(true);
}

What you need is something called Document Listener. See How to Write a Document Listener.


To Check JTextFiled is empty or not condition:

if( (billnotf.getText().length()==0)||(billtabtf.getText().length()==0))

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();

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");
    }
}

Well, the code that renders the button enabled/disabled:

if(name.getText().equals("")) {
    loginbt.setEnabled(false);
}else {
    loginbt.setEnabled(true);
}

must be written in javax.swing.event.ChangeListener and attached to the field (see here). A change in field's value should trigger the listener to reevaluate the object state. What did you expect?


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to swing

Calling another method java GUI Read input from a JOptionPane.showInputDialog box Call japplet from jframe Java JTable getting the data of the selected row What does .pack() do? How to add row of data to Jtable from values received from jtextfield and comboboxes How can I check that JButton is pressed? If the isEnable() is not work? Load arrayList data into JTable How to draw a circle with given X and Y coordinates as the middle spot of the circle? Simplest way to set image as JPanel background

Examples related to jbutton

Call japplet from jframe How can I check that JButton is pressed? If the isEnable() is not work? Java - Check if JTextField is empty or not how to create a window with two buttons that will open a new window How to close a GUI when I push a JButton? How to Disable GUI Button in Java How to add action listener that listens to multiple buttons Swing/Java: How to use the getText and setText string properly How to clear the JTextField by clicking JButton How do I add an image to a JButton

Examples related to jtextfield

Java - Check if JTextField is empty or not How to set the height and the width of a textfield in Java? getting integer values from textfield Restricting JTextField input to Integers Limiting the number of characters in a JTextField JTable - Selected Row click event How to Set Focus on JTextField? How to convert JTextField to String and String to JTextField? How to clear the JTextField by clicking JButton Detect enter press in JTextField

Examples related to documentlistener

Java - Check if JTextField is empty or not Value Change Listener to JTextField