Search code examples
javaswingfocusjcomponentsetfocus

Shifting focus from one component to another


How can I shift focus from one component to another component when I (program) experience's' a certain case ? Like the focus shifts to the next text-field when in the first text field the word length reaches 3.


Solution

    1. Subscribe to keyDown event on 1st component.

    2. if text length == 3 shift focus to another component. don't forget to undo the effect of current key down

    3. I guess focus() OR requestFocusInWindow() method can be used. don't remember exact name.

    Similarly, TextChanged event can be subscribed. so, ASA 3 chars are added, shift the focus using step 3.