javagwtgwt-extkeylistener

Handle callback for ENTER key in GWT-Ext's NumberField


I use GWT-Ext library for building GUI for a webapp. I'd like to process an ENTER key press inside of the NumberField.

It should be like this:

    final NumberField requiredHeight = new NumberField();
    requiredHeight.setValue(125);
    requiredHeight.setAllowBlank(false);
    requiredHeight.setAllowNegative(false);
    requiredHeight.setAllowDecimals(false);
    KeyListener listener = new KeyListener() {

        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode()==13)
                addPortlet(requiredHeight.getValue().intValue());
        }

    };
    requiredHeight.addKeyListener(listener);

But the code doesn't do anything! What am I doing wrong? Generally speaking, what is the best way to add such a handler to the field?


Solution

  • OK, solved that at last. There is another callback function inside of KeyListener - componentKeyPress instead of keyPressed. This is the correct code:

        KeyListener listener = new KeyListener() {
            @Override
            public void componentKeyPress(ComponentEvent event) {
                if(event.getKeyCode()==13)
                {
                    addPortlet(requiredHeight.getValue().intValue());
                }
                super.componentKeyPress(event);
            }
    
        };
        requiredHeight.addKeyListener(listener);