JavaFX input text box

I am using JavaFX and Scene Builder and I have a form with text boxes. Three of these text fields are processed from lines to doubles.

I want them to be school signs, so they should be allowed to be between 1.0 and 6.0. The user is not allowed to write something like "2.34.4", but something like "5.5" or "2.9" will be fine.

Validation for parsed fields:

public void validate(KeyEvent event) {
    String content = event.getCharacter();
    if ("123456.".contains(content)) {
            // No numbers smaller than 1.0 or bigger than 6.0 - How?
    } else {
        event.consume();
    }
}

      

How can I check if the user is entering the correct value?

I've already searched on Stackoverflow and on Google, but I haven't found a satisfying solution.

+3


source to share


2 answers


textField.focusedProperty().addListener((arg0, oldValue, newValue) -> {
        if (!newValue) { //when focus lost
            if(!textField.getText().matches("[1-5]\\.[0-9]|6\\.0")){
                //when it not matches the pattern (1.0 - 6.0)
                //set the textField empty
                textField.setText("");
            }
        }

    });

      

you can also change the template to [1-5](\.[0-9]){0,1}|6(.0){0,1}

, then 1,2,3,4,5,6

it will also be ok (not only 1.0,2.0,...

)



Update Here is a small test application with valid values โ€‹โ€‹of 1 (.00) to 6 (.00):

public class JavaFxSample extends Application {

@Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("Enter number and hit the button");
    GridPane grid = new GridPane();
    grid.setAlignment(Pos.CENTER);
    Label label1To6 = new Label("1.0-6.0:");
    grid.add(label1To6, 0, 1);
    TextField textField1To6 = new TextField();

    textField1To6.focusedProperty().addListener((arg0, oldValue, newValue) -> {
        if (!newValue) { // when focus lost
                if (!textField1To6.getText().matches("[1-5](\\.[0-9]{1,2}){0,1}|6(\\.0{1,2}){0,1}")) {
                    // when it not matches the pattern (1.0 - 6.0)
                    // set the textField empty
                    textField1To6.setText("");
                }
            }
        });
    grid.add(textField1To6, 1, 1);
    grid.add(new Button("Hit me!"), 2, 1);
    Scene scene = new Scene(grid, 300, 275);
    primaryStage.setScene(scene);
    primaryStage.show();
}

public static void main(String[] args) {
    launch(args);
}

}

      

+7


source


I would not advise you to use KeyEvent for this.

You should use a more classic way, such as confirming user input when the user has finished filling out a text box, or hitting the save button.



/**
 * Called this when the user clicks on the save button or finish to fill the text field.
 */
private void handleSave() {
        // If the inputs are valid we save the data
        if(isInputValid()){
            note=(DOUBLE.parseDouble(textField.getText()));
        }else // do something such as notify the user and empty the field
}

/**
 * Validates the user input in the text fields.
 * 
 * @return true if the input is valid
 */
private boolean isInputValid() {
    Boolean b= false;
    if (!(textField.getText() == null || textFiled.getText().length() == 0)) {
        try {
            // Do all the validation you need here such as
            Double d = Double.parseInt(textFiled.getText());
            if ( 1.0<d<6.0){
                b=true;
            }
        } catch (NumberFormatException e) { 
        }
    return b;
}

      

+4


source







All Articles