1

Heya guys! Nolankr here.

I've got this code that enables my password jField when I place something inside the username jField but when I delete my inputs from the username jField my password jField stays enabled. I wanted it to go back to being disabled though. I'm still a starter so I'm so sorry.

private void usernameKeyTyped(java.awt.event.KeyEvent evt) {                                  
        String usern = username.getText();

        if(usern != null){
            password.setEnabled(true);
        }else{
                password.setEnabled(false);

             }
    }   

I tried coding an infinite loop to it but it just made my .jar file to stop responding / won't close, so I had to close netbeans itself and restart it . xD

username and password are both jTextfields by the way and password is disabled by default

basically,
if username != null then enable password but if username = null again then disable password again

Nolan Kr
  • 81
  • 12

1 Answers1

1

What you probably want is a document listener that will allow you to detect when the username field is changed and take appropriate action.

I'm writing this answer with the mobile app so it's hard to provide a code sample right now.

Basically you would set up the listener on username to check if username is null or empty and enable/disable the password field based on the result of that check.

EDIT:

I'm back at my computer now, and am able to provide a code sample. See below:

userNameTextBox.getDocument().addDocumentListener(new DocumentListener(){

    @Override
    public void insertUpdate(DocumentEvent e) {
        handleTextChange();
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        handleTextChange();
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        //Do nothing here
    }

    private void handleTextChange(){
        if(userNameTextBox.getText() == null ||
            userNameTextBox.getText().trim().length() == 0){
            passwordBox.setEnabled(false);
        }else{
            passwordBox.setEnabled(true);
        }
    }

});

Note that the changedUpdate method does nothing because it is not fired when the document text changes, it is fired when attributes change. See the javadoc for complete details.

D.B.
  • 4,523
  • 2
  • 19
  • 39
  • So I've got this code snippet from another [thread](http://stackoverflow.com/questions/17132452/java-check-if-jtextfield-is-empty-or-not) on the answer portion. There seems to be errors from the code and it says that DocumentEvent is not found xD. Anyways, thanks for introducing me to document listeners. I'll study them asap ~ – Nolan Kr Nov 26 '16 at 23:44
  • Did you import the `DocumentEvent` class? – D.B. Nov 26 '16 at 23:47
  • Oh there's still something to import--- lemme try and get back – Nolan Kr Nov 26 '16 at 23:53
  • `username.getDocument().addDocumentListener(new DocumentListener());` this line of code has an error that says `expected ;` oh my God this is confusing. – Nolan Kr Nov 26 '16 at 23:58
  • Please read the link in my answer and see how that code sets up the listener – D.B. Nov 26 '16 at 23:59
  • Reading it ~ I think I'm almost there. I'll update when it's done. Thanks @D.B. !! – Nolan Kr Nov 27 '16 at 00:04