gwt click event document

心不动则不痛 提交于 2020-01-24 10:41:25

问题


i need to register a cross platform and version independent click event to the document. that means i have a two text box and submit button but when i click outside of the two text box and submit button then alert will be displayed .how can i achive this by gwt

document.get().addMouseClick ???


回答1:


The easiest way that comes to mind is to wrap everything in a FocusPanel:

ClickHandler clickHandler = new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            Window.alert("TextBox/Button clickHandler.");
            event.stopPropagation(); // The important line - We stop the event
                                     // propagation here so that the FocusPanel
                                     //  doesn't get the event
        }
    };
TextBox textBox = new TextBox();
textBox.addClickHandler(clickHandler);
Button button = new Button("Test");
button.addClickHandler(clickHandler);


// Since FocusPanel is a SimplePanel, it can only have one child, so we are
// wrapping everything additionally in a HorizontalPanel
HorizontalPanel hPanel = new HorizontalPanel();
hPanel.add(textBox);
hPanel.add(button);


FocusPanel focusPanel = new FocusPanel(hPanel);
focusPanel.addClickHandler(new ClickHandler() {
    @Override
    public void onClick(ClickEvent event) {
        Window.alert("Outside."); // Clicked outside of the TextBox/Button
    }
});

RootPanel.get().add(focusPanel);    

The downside is that you need to assign ClickHandlers to every element you don't want an alert for (you can use the same ClickHandler for that to save memory - like I did above). Other than that, the FocusPanel implementation should ensure that the onclick behavior stays cross-browser.



来源:https://stackoverflow.com/questions/2662877/gwt-click-event-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!