Search code examples
javaswingjtextfield

JTextField: focus next component when textfield is full / autoskip / Auto-Tabbing


I want to implement autoskip with JTextField, but don't know what is the best way to do this.

What is autoskip/Auto-Tabbing? When you reach the defined length limit of a textfield, you are automatically taken to the next field. (like pressing Tab, focus next component) Or what name do you use for this behaviour?

I tried this:

JTextField.getDocument.addChangeListener(): compare length and caret position. seems usable, but I can't distinguish typed user input from calls to JTextField.setText(String).

Focus should not be changed when text is changed by gui-refresh.

What do you think is the best way to implement this?


Solution

  • See Text Field Auto Tab.

    Focus should not be changed when text is changed by gui-refresh.

    a) removeListener
    b) setText
    c) addListener

    Edit:

    If you don't really like the concept of of needing the ChainDocumentFilter, then get rid of all references to that class. You can replace the provideErrorFeedback() method call with a Toolkit.beep() if you want.