JavaFX 2.2 TextField maxlength

后端 未结 9 1448
不知归路
不知归路 2020-12-03 11:47

I am working with a JavaFX 2.2 project and I have a problem using the TextField control. I want to limit the characters that users will enter to each TextField but I can\'t

9条回答
  •  独厮守ぢ
    2020-12-03 12:06

    private void registerListener1(TextField tf1, TextField tf2,TextField tf3,TextField tf4) {
        tf1.textProperty().addListener((obs, oldText, newText) -> {
    
            if(newText.length() == 12) {
    
                tf1.setText(newText.substring(0, 3));
                tf2.setText(newText.substring(tf1.getText().length(), 6));              
                tf3.setText(newText.substring(tf1.getText().length()+tf2.getText().length(), 9));
                tf4.setText(newText.substring(tf1.getText().length()+tf2.getText().length()+tf3.getText().length()));
                tf4.requestFocus();
            }
        });
    
    }
    
    private void registerListener(TextField tf1, TextField tf2) {
        tf1.textProperty().addListener((obs, oldText, newText) -> {
            if(oldText.length() < 3 && newText.length() >= 3) {
                tf2.requestFocus();
            }
        });
    
    }
    

提交回复
热议问题