33

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);
    }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
ColdStormy
  • 562
  • 1
  • 6
  • 15

12 Answers12

38

For that you need to add change listener (a DocumentListener which reacts for change in the text) for your JTextField, and within actionPerformed(), you need to update the loginButton to enabled/disabled depending on the whether the JTextfield is empty or not.

Below is what I found from this thread.

yourJTextField.getDocument().addDocumentListener(new DocumentListener() {
  public void changedUpdate(DocumentEvent e) {
    changed();
  }
  public void removeUpdate(DocumentEvent e) {
    changed();
  }
  public void insertUpdate(DocumentEvent e) {
    changed();
  }

  public void changed() {
     if (yourJTextField.getText().equals("")){
       loginButton.setEnabled(false);
     }
     else {
       loginButton.setEnabled(true);
    }

  }
});
Community
  • 1
  • 1
sanbhat
  • 17,522
  • 6
  • 48
  • 64
  • I wanted to ask whether the procedure changes if the button were to depend on more than 1 text field. Would I use the same logic to define the action or is there some other way with less code? Thanks for your time!! – boxed__l Jan 13 '14 at 22:27
  • Then you create one `DocumentListener` implementation which handles that button and add them as listeners to all the textboxes? – sanbhat Jan 14 '14 at 05:15
  • yes..thanks..forgot basic stuff: [how to write document listener](http://docs.oracle.com/javase/tutorial/uiswing/events/documentlistener.html) – boxed__l Jan 14 '14 at 12:11
22

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

name.getText().isEmpty();

loggeek
  • 123
  • 1
  • 9
xulo
  • 440
  • 3
  • 9
4

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

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
4

To Check JTextFiled is empty or not condition:

if( (billnotf.getText().length()==0)||(billtabtf.getText().length()==0))
László Papp
  • 51,870
  • 39
  • 111
  • 135
3

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?

darijan
  • 9,725
  • 25
  • 38
2

use the following code :

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

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();
Juan-Kabbali
  • 1,961
  • 15
  • 20
0

Try this

if(name.getText() != null && name.getText().equals(""))
{
        loginbt.setEnabled(false);
}
else
{
        loginbt.setEnabled(true);
}
Aneeq Anwar
  • 1,282
  • 7
  • 20
0
if(name.getText().hashCode() != 0){
    JOptionPane.showMessageDialog(null, "not empty");
}
else{
    JOptionPane.showMessageDialog(null, "empty");
}
mathielo
  • 6,725
  • 7
  • 50
  • 63
Sijo Jose
  • 363
  • 1
  • 3
  • 10
0

Try this one and change booleans statements around.

String text = name.getText(); 
if(!text.isEmpty()) {
    loginbt.setEnabled(true);
}else {
    loginbt.setEnabled(false);
}

In my action method it worked fine.

public void actionPerformed(ActionEvent e){
    String inputText = e.getActionCommand();
    String itext = text1.getText();
    if(inputText.equals("Submit") && !itext.isEmpty()){
        label1.setText(text1.getText());
        text1.setText("");
    }
}
elgijo421
  • 1
  • 1
-1

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) {
        }

    });
Nikos
  • 1
  • 1
    -1 A DocumentListener is the preferred approach. There is rarely a reason to use a KeyListener. That is old design when using AWT. Also the code would not work anyway because no text has been added to the Document when the keyPressed event is generated, so the length will still be 0 when the first event is fired. – camickr Jun 16 '13 at 16:39
  • 1
    Thanks for the correction,i will see the DocumentListener in the future – Nikos Jun 16 '13 at 19:47
-1
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");
    }
}
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
  • Hi @HarinduLakshan, please consider adding more information to improve the quality of the answer. – Tiago Martins Peres Dec 21 '19 at 12:43
  • This answer has multiple issues. It doesn't clearly address the question, the variable names don't adhere to Java conventions, and it uses hashCode() rather than isEmpty() to check for missing values. It also contains unnecessary comments. – Greg Brown Nov 27 '22 at 14:04