JTextField: focus next component when text field is filled / autoskip / Auto-Tabbing

I want to implement autoskip with JTextField, but don't know how best to do this.

What is autoskip / auto-tabbing? When you reach the limit of a certain length of the text field, you will automatically move to the next field. (e.g. pressing Tab, focus of the next component) Or what name do you use for this behavior?

I tried this:

JTextField.getDocument.addChangeListener(): Compare the length and position of the carriage. seems convenient, but I cannot distinguish user input from calls JTextField.setText(String).

Focus should not change when changing text with gui-refresh.

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

+3
source share
3 answers

. .

gui-refresh.

a) removeListener
) setText
c) addListener

Edit:

ChainDocumentFilter, . ErrorFeedback() Toolkit.beep(), .

+1

, KeyListener . , .

:

addKeyListener(new KeyAdapter(){
    public void keyTyped(KeyEvent e) {
        if (getText().length() >= MAX_LENGTH) {
            // Move the focus
        }
    }
});

:
@camickr:

private DocumentListener myTabChangeListener;
@Override
public void setText(String text) {
    getDocument().removeDocumentListener(myTabChangeListener);
    super.setText(text);
    getDocument().addDocumentListener(myTabChangeListener);
}
+3

In your listener, create the isAPI flag to distinguish if your code calls setText (). Set the flag to true before calling setText () and reset after it.

When it’s true, do nothing and move the focus in the opposite case.

0
source

Source: https://habr.com/ru/post/1788204/


All Articles