Advice needed with disabling a JButton until required fields have been filled in - Java

java, jbutton, textfield

Solution

Add a KeyListener like this to your fields:

nameField.addKeyListener(new KeyAdapter() {
        public void keyReleased(KeyEvent e) {
            super.keyReleased(e);
            if(nameField.getText().length() > 0 && surNameField.getText().length() > 0)
                nextButton.setEnabled(true);
            else
                nextButton.setEnabled(false);
        }
    });

This will check if the two fields are not empty, every time a key has been pressed in the field. If the condition is true, the next button will be enabled.

Problem

I am making a courier program in which the user can create a new account. I want to have a next button that the user cannot press until the text fields have been filled in. So a blank Name Field and Surname field would mean the next button cannot be pressed but if the fields have been filled in, the next button can be pressed

Original source