Size/Center a Dialog

1,395 views
Skip to first unread message

Eric B

unread,
Sep 1, 2007, 4:17:20 AM9/1/07
to Google Web Toolkit
I've reviewed some of the past posts about sizing and centering a
Dialog object, but none of the solutions are really working well
enough for me. Does anyone have a elegant solution to sizing,
centering, then showing a Dialog?

Thanks,
Eric

Peter Blazejewicz

unread,
Sep 1, 2007, 5:26:13 PM9/1/07
to Google Web Toolkit
hi Eric,

add your content and then try following solution based on GWT docs:

package com.mycompany.project.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class TestDialog implements EntryPoint {
private Button clickMeButton;

public void onModuleLoad() {
RootPanel rootPanel = RootPanel.get();
clickMeButton = new Button("Open sample dialog");
clickMeButton.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
// show example dialog
new ExampleDialog();
}
});
rootPanel.add(clickMeButton);
}

class ExampleDialog extends DialogBox {
public ExampleDialog() {
setText("Example dialog text");
final Button closeBtn = new Button("CLOSE");
closeBtn.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
hide();
}
});
setWidget(closeBtn);
// show and center
setPopupPositionAndShow(new PopupPanel.PositionCallback() {
public void setPosition(int offsetWidth, int offsetHeight) {
int left = ((Window.getClientWidth() - offsetWidth) / 2) >> 0;
int top = ((Window.getClientHeight() - offsetHeight) / 2) >> 0;
setPopupPosition(left, top);
}
});
}
}
}

regards,
Peter

Eric B

unread,
Sep 1, 2007, 6:20:51 PM9/1/07
to Google Web Toolkit

Filipe Sousa

unread,
Sep 2, 2007, 7:48:57 AM9/2/07
to Google-We...@googlegroups.com
Peter Blazejewicz wrote:
> hi Eric,
>
[...]

> }
>
> class ExampleDialog extends DialogBox {
> public ExampleDialog() {
> setText("Example dialog text");
> final Button closeBtn = new Button("CLOSE");
> closeBtn.addClickListener(new ClickListener() {
> public void onClick(Widget sender) {
> hide();
> }
> });
> setWidget(closeBtn);
> // show and center
> setPopupPositionAndShow(new PopupPanel.PositionCallback() {
> public void setPosition(int offsetWidth, int offsetHeight) {
> int left = ((Window.getClientWidth() - offsetWidth) / 2) >> 0;
> int top = ((Window.getClientHeight() - offsetHeight) / 2) >> 0;
> setPopupPosition(left, top);
> }
> });
> }
> }
> }

why not new ExampleDialog().center()?

- Filipe Sousa

signature.asc

Eric B

unread,
Sep 2, 2007, 1:35:55 PM9/2/07
to Google Web Toolkit
Filipe,
Wow, you're right, there is a center() method too.
http://code.google.com/webtoolkit/documentation/com.google.gwt.user.client.ui.PopupPanel.html#center()

However, I only want to center the dialog horizontally, not
vertically, so Peter's suggestion is what I'll continue to use. I
think center() will work for a lot of other people though.

Thanks,
Eric

Axel Kittenberger

unread,
Sep 2, 2007, 3:55:39 PM9/2/07
to Google Web Toolkit
get top position
center()
set top position

-> centered horizontally but not vertically

On Sep 2, 7:35 pm, Eric B <ebesse...@gmail.com> wrote:
> Filipe,

> Wow, you're right, there is a center() method too.http://code.google.com/webtoolkit/documentation/com.google.gwt.user.c...()

Eric B

unread,
Sep 2, 2007, 5:47:01 PM9/2/07
to Google Web Toolkit
Yep, that's another way to do it.

Peter Blazejewicz

unread,
Sep 2, 2007, 5:47:28 PM9/2/07
to Google Web Toolkit
hi Axel,

On Sep 2, 9:55 pm, Axel Kittenberger <axe...@gmail.com> wrote:
> get top position
> center()
> set top position


there will be flickrering with that multistep positioning I think,
I've used code recommended to avoid flickering simply,
regards,
peter


Reply all
Reply to author
Forward
0 new messages