现在我可以做到
public TextField lastmonthbox;
public void initialize()throws Exception{
lastmonthbox.addEventFilter(KeyEvent.KEY_TYPED, new EventHandler<KeyEvent>() {
public void handle( KeyEvent t ) {
char ar[] = t.getCharacter().toCharArray();
char ch = ar[t.getCharacter().toCharArray().length - 1];
if (!(ch >= '0' && ch <= '9' || ch=='.')) {
System.out.println("The char you entered is not a number");
t.consume();
}
}
});
}
有没有办法通过fxml添加事件过滤器,看起来像onAction =“#someFunction”??
我没有找到任何关于 fxml 中所有可能标签的列表或教程(
最佳答案
据我所知不可以,但您始终可以根据您的需要自定义控件。例如像
<MyTextField ... onKeyTypedFilter="#yourFilter" />
然后在后端做一些反射工作。但这值得吗?
关于java - 通过fxml添加EventFilter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22589901/