0

I am developing a Java Swing Application.

The application ends with a form to take the customers information (name, last name, email, gender, etc)

I am trying to validate the text fields in a way that when the application is launched focus will go on the first text box (first name). I want the user to not be able to leave the box until they have entered a valid name (no numbers, punctuation and so on).

So far I am using JOptionPane.showMessageDialog(null, "") to output messages to the user, and the textfields are rigged with lost focus listeners.

Unfortunately even after looking online, I am not able to find a solution to validating these boxes.

Action Plan:

Focus on first textfield >>> Unable to leave unless valid input is entered >> messages outputted to user >> validation completed >> focus set on next field >> and so on.

Help would be appreciated.

Frakcool
  • 10,915
  • 9
  • 50
  • 89
JSint
  • 101
  • 1
  • 11

1 Answers1

-1

Try something along the lines of the following code for listener

class CustomFocusListener implements FocusListener
{
    @Override
    public void focusGained(FocusEvent e)
    {

    }

    @Override
    public void focusLost(FocusEvent e)
    {
        validateText();
        if(validateFailed) {
            myJTextField.grabFocus();
        }
    }
}

As for upon launch just try

myFirstJTextField.grabFocus();

Look into JComponent.grabFocus() for more information.

Dan
  • 7,286
  • 6
  • 49
  • 114