开发者

How to add a close button to the caption bar of a GWT DialogBox [duplicate]

This question already has answers here: GWT Close button in title bar of DialogBox (14 answers) 开发者_开发百科 Closed 9 years ago.

I need to add a close button to the caption bar of my dialog box . I'm able to place a widget in the caption bar , but not been able to get the events for it .


http://zone817.blogspot.com/2010/08/close-button-in-caption-bar-of-gwt.html

seems like exactly what you want.


Here is a modification of the example given by amal. This code retains gwt-DialogBox caption style.

public class CloseButtonDialogBox extends DialogBox {

private Node closeEventTarget = null;

public CloseButtonDialogBox() {
    // get the "dialogTopRight" class td
    Element dialogTopRight = getCellElement(0, 2);

    // close button image html
    dialogTopRight.setInnerHTML(
            "<div style=\"margin-left:-25px;margin-top: 7px;\">" + 
            "<img src=\"images/closebutton.png\" height=\"20px\"/>" + 
            "</div>");

    // set the event target
    closeEventTarget = dialogTopRight.getChild(0).getChild(0);
}

@Override
protected void onPreviewNativeEvent(NativePreviewEvent event) {
    NativeEvent nativeEvent = event.getNativeEvent();

    if (!event.isCanceled() 
            && (event.getTypeInt() == Event.ONCLICK)
            && isCloseEvent(nativeEvent))
    {
        this.hide();
    }
    super.onPreviewNativeEvent(event);
}

// see if the click target is the close button
private boolean isCloseEvent(NativeEvent event) {
    return event.getEventTarget().equals(closeEventTarget); //compares equality of the underlying DOM elements
}   


See the answer posted in the linked question. It shows the proper way to do this by implementing the DialogBox.Caption interface and then adding an event handler to your caption implementation for the included close button that hides the dialog box.

This did the trick for me.

https://stackoverflow.com/a/7960172/458426

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜