Range of Values in JavaFx JtextField

0

Good and thanks in advance for the help, I need to restrict the values inserted in a JTextField to> 0 and

asked by Raul.Rt 16.11.2017 в 17:35
source

2 answers

2

A widely used solution is to create your own TextFormatter , which restricts the entry of user data and prevents the text from reaching the TextField , so that a higher performance is achieved than launching an event:

@FXML
public TextField mytext;

String regex = "10|[1-9]";

public void initialize(URL location, ResourceBundle resources) {
    TextFormatter<String> formatter = new TextFormatter<String>(change -> {
        String text = change.getControlNewText();
        if (!Pattern.matches(regex, text)) {
            change.setText("");
        }
        return change;
    });
    mytext.setTextFormatter(formatter);
}

In this case, the data entry is restricted from a regular expression ( 10|[1-9] ) that accepts the number 10 or the numbers between 1 and 9.

    
answered by 16.11.2017 / 18:11
source
0

This can help you:

    private JTextField jTextFieldName =new JTextField();

    private int limite  = 8;

    jTextFieldName.addKeyListener(new KeyListener(){

       public void keyTyped(KeyEvent e){
             if (jTextFieldName.getText().length() == limite)  
             e.consume();
       }

    });
    
answered by 16.11.2017 в 17:50