【问题标题】:Change text in TextFormatter results a recursive call更改 TextFormatter 中的文本会导致递归调用
【发布时间】:2016-03-04 13:27:23
【问题描述】:

我有一个 java fxml 应用程序,它有一个字段,用户可以在其中输入他的 iban 号码。

4个字符后会自动添加一个空格。

public TextFormatter.Change apply(TextFormatter.Change t) {
    String text = t.getControlNewText();
    if(text.length() > 0 && text.length() % 4 == 0)
        t.setText(text + " ");
    return t;
}

但是当我输入 1234 时,它返回 1231234 所以我认为当我使用t.setTextfunction 时也会触发 change 事件。

是否可以阻止此调用或无法在 TextFormatter 中格式化文本?

【问题讨论】:

    标签: java fxml text-formatting


    【解决方案1】:

    在将text 设置为TextFormatter 之前,尝试在text 后面加上空白

    if(text.length() > 0 && text.length() % 4 == 0){
            String txt = text + " ";
            t.setText(txt);
    }
    

    【讨论】:

    • 不起作用。将其设置为变量或直接将其提供给函数是相等的。
    猜你喜欢
    • 2020-07-16
    • 2021-04-03
    • 2016-05-24
    • 2014-01-10
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    • 2016-06-02
    相关资源
    最近更新 更多