2017-04-07 40 views
0

我想在JavaFX中只接受整数值创建一个TextArea?任何人都可以给我如何实施这个建议吗?如何在JavaFX中设置TextArea以仅允许输入整数?

+1

见链接的问题 - 应该'TextArea'工作同样一样的'TextField'。 – Itai

+1

假设在一个'TextArea'中,你至少想要允许换行符,并且可能通常使用空格,所以你需要修改那个回答中的正则表达式。 –

回答

1

使用TextFormatter

TextArea textArea = new TextArea(); 
// allow digits and whitespace: 
Pattern allowedText = Pattern.compile("[0-9\\s]*"); 
TextFormatter formatter = new TextFormatter((TextFormatter.Change c) -> { 
    if (allowedText.matcher(c.getText()).matches()) { 
     return c ; 
    } else { 
     return null ; 
    } 
}); 
+0

非常感谢! – onagh

相关问题