JavaFX 2.2 TextField maxlength

后端 未结 9 1481
不知归路
不知归路 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 11:58

    I have this bit of code that only allows numbers and limits the input length on a text field in Javafx.

    // Event handler for inputPrice
         inputPrice.setOnAction(event2 -> {
    
                // Obtain input as a String from text field
                String inputPriceStr = inputPrice.getText();
    
                // Get length of the String to compare with max length
                int length = inputPrice.getText().length();
    
                final int MAX = 10; // limit number of characters
    
                // Validate user input allowing only numbers and limit input size
                if (inputPriceStr.matches("[0-9]*") && length < MAX ) {
    
                     // your code here
                 }});
    

提交回复
热议问题