3

I need to print something in the method on every key press event. I have tried the below code and the problem with that is, first key press in always returning null. Whereas, after typing the second letter, it prints the first key event. Key Press event is not capturing the letter on first event. Can you please help in resolving this ?

final StringComboBox searchGridTextBox = new StringComboBox();

searchGridTextBox.setEmptyText("Search Grid");
searchGridTextBox.addFocusHandler(new FocusHandler(){
    @Override
    public void onFocus(FocusEvent event){
        if(searchGridTextBox.getStore().size() > 0)
            searchGridTextBox.expand();

    }
}); 
searchGridTextBox.addKeyPressHandler(new KeyPressHandler() {
    @Override
    public void onKeyPress(KeyPressEvent event) {
        System.out.println("On key press event ")   ;           
    }
});
François Maturel
  • 5,884
  • 6
  • 45
  • 50
Swats
  • 68
  • 7

2 Answers2

1

For this scenario, you need to use KeyUpEvent. Please find the updated code below.

final StringComboBox searchGridTextBox = new StringComboBox();

searchGridTextBox.setEmptyText("Search Grid");
searchGridTextBox.addFocusHandler(new FocusHandler(){
@Override
public void onFocus(FocusEvent event){
    if(searchGridTextBox.getStore().size() > 0)
        searchGridTextBox.expand();

}
}); 
searchGridTextBox.addKeyUpHandler(new KeyUpHandler() {
@Override
public void onKeyUp(KeyUpEvent event) {
    System.out.println("On key up event ")   ;           
}
});
Kutty
  • 712
  • 1
  • 5
  • 22
0

There are 2 more handlers available keyUp and keyDown handler. Try using keyUp/keyDown handler and see if it fulfills your requirements.

There is a difference how keyPress behaves in a case of empty combo box which is explained in this post:

https://stackoverflow.com/a/42036960/3612019

Community
  • 1
  • 1