JavaFX TextField: Alternative for Swing InputVerifier?

前端 未结 2 1163
情歌与酒
情歌与酒 2021-01-25 08:44

I am looking for an alternative of Swing InputVerifier to JavaFX TextField.

The Swing InputVerifier will prevent input that does verify.

Consider the following S

2条回答
  •  情深已故
    2021-01-25 09:39

    As first suggested by James_D a TextFormatter with filter was the solution.

    TextField instance = new TextField();
    UnaryOperator textFilter = change -> {
        String input = change.getText();
        if (!change.isContentChange()) {
            return change;
        }
        if (!myRegExTool.matches(input)) {
            return null;
        }
        return change;
    };
    instance.setTextFormatter(new TextFormatter(textFilter));
    

提交回复
热议问题