gpt4 book ai didi

java - 在 JavaFX 8 DatePicker 中处理 ENTER 键按下

转载 作者:行者123 更新时间:2023-11-30 11:20:56 24 4
gpt4 key购买 nike

作为用户的必要条件,我在我的 JavaFX 应用程序中将 ENTER 实现为 TAB。我正在使用以下代码来识别所有 Control存在于 Pane 中并添加 OnKeyPressed处理程序:

protected EventHandler<KeyEvent> processadorEnterEmCampo = new EventHandler<KeyEvent>() {
public void handle(final KeyEvent evento) {
if (evento.getCode() == KeyCode.ENTER) {
evento.consume();
((Node)evento.getSource()).fireEvent(new KeyEvent(evento.getSource(), evento.getTarget(), evento.getEventType(), null, "TAB", KeyCode.TAB, false, false, false, false));
}
}
};

private void adicionarProcessadorEventoEnterPressionado(Node elemento) {
if(elemento instanceof Pane){
Pane painel= (Pane) elemento;
for(Node filho :painel.getChildren()){
if(filho instanceof TextField || filho instanceof ComboBox || filho instanceof CheckBox
|| filho instanceof DatePicker || filho instanceof BigDecimalField)
filho.setOnKeyPressed(processadorEnterEmCampo);
else if(filho instanceof Button)
filho.setOnKeyPressed(processadorEnterEmBotao);
else
adicionarProcessadorEventoEnterPressionado(filho);
}
}
}

除了 BigDecimalField 和 DatePicker 之外,上面的代码运行起来很顺利。 .当我按下 ENTER 键时,它根本不运行处理程序的代码,只有当我按下 SHIFT 键时,处理程序的代码才会执行​​。我相信这种情况正在发生,因为这些组件已经具有一些带有 ENTER 键的功能。我可以做些什么来处理这些组件中的 ENTER 键按下?

最佳答案

我现在使用 addEventFilter 而不是使用 setOnKeyPressed :

private void adicionarProcessadorEventoEnterPressionado(Node elemento) {
if(elemento instanceof Pane){
Pane painel= (Pane) elemento;
for(Node filho :painel.getChildren()){
if(filho instanceof TextField || filho instanceof ComboBox || filho instanceof CheckBox
|| filho instanceof DatePicker || filho instanceof BigDecimalField)
filho.addEventFilter(KeyEvent.KEY_PRESSED,processadorEnterEmCampo);
else if(filho instanceof Button)
filho.setOnKeyPressed(processadorEnterEmBotao);
else
adicionarProcessadorEventoEnterPressionado(filho);
}
}
}

因为我怀疑组件的实现在事件到达处理程序之前消耗了事件,所以 addEventFilter 是最好的选择:

The filter is called when the node receives an Event of the specified type during the capturing phase of event delivery.

关于java - 在 JavaFX 8 DatePicker 中处理 ENTER 键按下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22480551/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com