简体   繁体   English

处理ENTER键并按下JavaFX的8个DataPicker

[英]Handling ENTER key pressing in JavaFX's 8 DataPicker

I'm implementing ENTER as TAB in my JavaFX application, as a requisite of users. 作为用户的要求,我正在JavaFX应用程序中将ENTER作为TAB实现。 I'm using the following code to identify all Control that exists in a Pane and add a OnKeyPressed handler: 我正在使用以下代码来标识窗格中存在的所有控件 ,并添加一个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);
        }
    }
}

The above code runs like a charm, except for BigDecimalField and DatePicker . 除了BigDecimalField和DatePicker以外,上面的代码运行起来很吸引人 It simply don't runs handler's code when I press ENTER key, only when I press SHIFT key the handler's code is executed. 当我按ENTER键时,它根本不运行处理程序的代码,只有当我按SHIFT键时,处理程序的代码才会执行​​。 I believe this is happening because these components already have some functionality with ENTER key. 我相信这种情况正在发生,因为这些组件已经具有使用ENTER键的某些功能。 What I could do to handle ENTER key press in these components? 如何处理这些组件中的ENTER键?

Instead of use setOnKeyPressed, now I'm using addEventFilter : 现在我不再使用setOnKeyPressed,而是使用addEventFilter

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);
        }
    }
}

As I was suspecting that the implementation of the components was consuming the event before it gets to the handler, addEventFilter was the best bet: 由于我怀疑组件的实现会在事件到达处理程序之前就占用了该事件,因此addEventFilter是最好的选择:

The filter is called when the node receives an Event of the specified type during the capturing phase of event delivery. 当节点在事件传递的捕获阶段中收到指定类型的事件时,将调用过滤器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM