Stop a event from bubbling in GWT

I have the following snippet of code, changeTextArea is a TextArea object.

changeTextArea.addKeyboardListener(new KeyboardListenerAdapter()
  public void onKeyPress( Widget sender, char keyCode, int modifier){
    //do something
    //I WISH TO STOP THE EVENT THAT MAPS TO THIS KEYPRESS FROM BUBBLING ANY FURTHER
  }
}

How would I stop the Event that is causing this method to be called from bubbling up from changeTextArea into the Panels/Widgets/Composites/Whatever that contain changeTextArea. Put succinctly, how do I stop it from bubbling any further. Any help would be appreciated (especially code samples).


Asked by: Rafael210 | Posted: 28-01-2022






Answer 1

As far as I know you can't do it via a keyboard listener, but it is possible by adding an event preview using the DOM class:

DOM.addEventPreview(EventPreview preview) 

Then when you get the event:

onEventPreview(Event event) 

You should return false, to say you want to cancel the event. The Event object also supports this method:

public final void cancelBubble(boolean cancel)

Cancels bubbling for the given event. This will stop the event from being propagated to parent elements.

You can find more details here: http://google-web-toolkit.googlecode.com/svn/javadoc/1.5/index.html?overview-summary.html

Answered by: Julia954 | Posted: 01-03-2022



Answer 2

You can definitely use the Event's cancelBubble() and preventDefault() methods from within any code that has access to the Event. There's no need to have an event preview...

Answered by: Melanie154 | Posted: 01-03-2022



Answer 3

You can call the sender's cancelKey() event. Here's an example that will only allow numbers to be inputted, all other keys get rejected.

private class RowColChangeHandler implements KeyPressHandler {

    public void onKeyPress(KeyPressEvent event) {
        char keyCode = event.getCharCode();
        if(keyCode <48 || keyCode >57)
        {
            ((TextArea)event.getSource()).cancelKey();
        }
    }
}

Answered by: Julian280 | Posted: 01-03-2022



Answer 4

you could reach it when possible by doing event.doit = false

Answered by: Chelsea857 | Posted: 01-03-2022



Similar questions

java - Prevent Enter Event from Bubbling to to Main Window

I have a custom Swing component called NavigationLink which extends JLabel and implements a key event listener like so: addKeyListener(new KeyAdapter() { public void keyReleased(KeyEvent e) { boolean actionInvoked = e.getKeyCode() == KeyEvent.VK_ENTER || e.getKeyCode() == KeyEvent.VK_SPACE; if (actionInvoked &amp;&amp; NavigationLink.this.clickAction != null) { NavigationLink...


Code style Java: bubbling up return value from private method

I often face the situation where I want to do some calculations in a private method and bubble up the return of the private method when it is not null. I feel there should be a nicer way than doing this: public String myMethod(int value) { String tmp = strategy1(value); if (tmp != null) { return tmp; } tmp = strategy2(value) if (tmp != null) { return tmp; } return...






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top