GWT Glass Pane

This code darkens the entire screen, allowing you to create lightbox effects when showing dialogs. For example, here is the normal screen:

Lightbox 1

And here is how the screen looks with a glass pane and dialog:

Lightbox 2

There are GWT component libraries that do this sort of thing, for example GlassPanel, but I prefer to minimize external dependencies. Instead, I decided to try this code example by Evaldas Taroza.

My Changes

I made a few changes to the original code:

  • Since I’m using GWT 1.7, I implement ResizeHandler instead of WindowResizeListener, which is deprecated.
  • I’m removing the -moz-opacity since it is specific to older versions of Mozilla and no longer seems relevant.

The Final Code

First, here is the CSS file:

.gcomp-GlassPanel {
    background-color: black;
    filter: alpha(opacity=50);
    opacity: .50;
}

Next, here is the JavaDoc comment, listed separately because syntax highlighting wasn’t working here on my blog.

/**
 * This panel is positioned absolutely and covers the whole client
 * area of the browser. It can be used to disable everything
 * underneath it with appropriate z-index.
 * <p/>
 * For instance a dialog can attach it to the root panel during
 * opening, and remove it during closing.
 * <p/>
 * CSS:<br>
 * .gcomp-GlassPanel
 * <p/>
 * Example:<br>
 * Add and remove this widget whenever you want, and style it as
 * follows:
 * <pre>
 * .gcomp-GlassPanel{
 *   background-color: black;
 *   filter:alpha(opacity=50);
 *   opacity:.50;
 * }
 * </pre>
 */

And here is the code…

public class GlassPanel extends Composite implements ResizeHandler {
    public static final String STYLE = "gcomp-GlassPanel";

    private SimplePanel panel = new SimplePanel();

    public GlassPanel() {
        initWidget(panel);

        panel.setStylePrimaryName(STYLE);
        Window.addResizeHandler(this);
        resize();
    }

    public void onResize(ResizeEvent event) {
        resize();
    }

    public void show() {
        // Override the styles explicitly, because it's needed
        // every time the widget is detached
        Element elem = panel.getElement();
        DOM.setStyleAttribute(elem, "left", "0");
        DOM.setStyleAttribute(elem, "top", "0");
        DOM.setStyleAttribute(elem, "position", "absolute");
        RootPanel.get().add(this);
    }

    public void hide() {
        RootPanel.get().remove(this);
    }

    private void resize() {
        panel.setSize(Window.getClientWidth() + "px",
                Window.getClientHeight() + "px");
    }
}

I’ve only done minimal testing, on the latest Firefox and IE7.


What is “MozOpacity” supposed to be? The site you link to just says “-moz-opacity is deprecated, use opacity”, nowhere is MozOpacity mentioned.

Othe then that I wonder if those properties should be in a stylesheet in the first place.

Eric Burke Says:

Sorry about the confusion. If you use CSS, you can use -moz-opacity. If you set the CSS programmatically via GWT’s DOM class, you must camel-case it to “MozOpacity”. I believe the CamelCaseRequirement was introduced in GWT 1.6.

I’ll have to research the stylesheet issue. My intent was to set up the default properties right here in the code, so 99% of the time you don’t need CSS at all. I’m assuming you could still customize the properties via CSS, but I need to confirm that.

You’d have to use !important in your stylesheet to override the inline styles, not a great solution.

Eric Burke Says:

Thanks for your tips, I’ve updated the code to remove the hard-coded DOM usage.

Pace Says:

Will this block override the click handling of anything behind the Glass Panel?

Eric Burke Says:

@Pace I’m using this in combination with normal GWT dialogs, so the dialog blocks all events to other components. The glass pane just darkens the screen.

Without a dialog, I’m not sure if any events would be blocked. I suspect not, but can’t say for sure.

They won’t ;)

Moe Says:

I designed this CSS for underlays:

.dialogUnderlay {
position: fixed;
_position: absolute;
_height:100%;
top: 0;
left: 0;
bottom: 0; /* http://www.alistapart.com/articles/conflictingabsolutepositions */
width: 100%;
z-index: 998;
display: none;
background-color: #88c;
opacity: 0.4;
filter: progid:DXImageTransform.Microsoft.Alpha(opacity=40);
}

Leave a Reply