我正在尝试使用可编辑的组合框。在那里我想添加一个用于按ENTER键的监听器。我尝试了以下选项,但没有一个工作。:(
cmb_year
是组合框对象。
cmb_year.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent t) {
if (t.getCode() == KeyCode.ENTER) {
System.out.println("Entered");
} else if (t.getCode() == KeyCode.ESCAPE) {
System.out.println("Entered");
}else
{
}
}
});
cmb_year.getEditor().setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent t) {
if (t.getCode() == KeyCode.ENTER) {
System.out.println("Entered");
} else if (t.getCode() == KeyCode.ESCAPE) {
System.out.println("Entered");
}else
{
}
}
});
答案 0 :(得分:0)
您可以使用此代码:
cmb_year.addEventFilter(KeyEvent.KEY_PRESSED,new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent t) {
if (t.getCode() == KeyCode.ENTER) {
System.out.println("Entered");
} else if (t.getCode() == KeyCode.ESCAPE) {
System.out.println("Entered");
}else
{
}
}
});