I need resize popup on browser window resize. I added ResizeHandler in my popup constructor, but after several browser resizing center() function create new popup, instead of centering the current. Here some code what i have tried already. Please tell me how to solve this or suggest some solutions.
public BigPopup() {
...
final BigPopup self = this;
Window.addResizeHandler(new ResizeHandler() {
@Override
public void onResize(ResizeEvent event) {
self.setHeight(getNewHeight());
self.setWidth(getNewWidth());
self.center();
}
});
...
}
public BigPopup() {
...
Window.addResizeHandler(new ResizeHandler() {
@Override
public void onResize(ResizeEvent event) {
BigPopup.this.setHeight(getNewHeight());
BigPopup.this.setWidth(getNewWidth());
BigPopup.this.center();
}
});
...
}
Added:
I created a simple project that illustrates the problem: Class of Popup
package tesr.client;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;
public class BigPopup extends PopupPanel {
private static BigPopupUiBinder uiBinder = GWT
.create(BigPopupUiBinder.class);
interface BigPopupUiBinder extends UiBinder<Widget, BigPopup> {
}
@UiField
Button tstBtn;
@UiHandler("tstBtn")
void click(ClickEvent event) {
this.hide();
}
public int[] getSize() {
int[] mas = new int[2];
int x = Window.getClientWidth();
int y = Window.getClientHeight();
if (x >= 1024) {
mas[0] = x - 100;
mas[1] = y - 100;
} else {
mas[0] = 1024;
mas[1] = 768;
}
return mas;
}
public BigPopup() {
setWidget(uiBinder.createAndBindUi(this));
Window.addResizeHandler(new ResizeHandler() {
@Override
public void onResize(ResizeEvent event) {
BigPopup.this.setHeight(getSize()[1] + "px");
BigPopup.this.setWidth(getSize()[0] + "px");
BigPopup.this.center();
}
});
this.setHeight(getSize()[1] + "px");
this.setWidth(getSize()[0] + "px");
this.setAnimationEnabled(true);
this.setGlassEnabled(true);
this.setAutoHideEnabled(false);
}
}
XML for uibinder
<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
xmlns:g="urn:import:com.google.gwt.user.client.ui">
<ui:style>
</ui:style>
<g:HTMLPanel>
<g:Label text="testLabel"></g:Label>
<g:Button text="testButton" ui:field="tstBtn"></g:Button>
</g:HTMLPanel>
</ui:UiBinder>
and main class
package tesr.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
public class AAA implements EntryPoint {
public void onModuleLoad() {
Button btn = new Button("Show Popup");
btn.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
BigPopup popup = new BigPopup();
popup.center();
}
});
RootPanel.get().add(btn);
}
}
What i making wrong? Or maybe it's bug of GWT?
The problem probably doesn't have anything to do with the resizing at all. It's just this:
So we have two popups, and two ResizeHandlers.
Now, on resize, you call 'center()' (which happens in both ResizeHandlers) - which has the side effect, that it shows the popups (if they aren't currently shown). The first popup is currently detached from the DOM, but GWT is smart enough to re-attach it. But now you see them both at once.
The solution is to remove the resize handler, when you hide the popup.
You may wonder, that there is no com.google.gwt.user.client.Window.removeResizeHandler()
method. It works a little bit differently:
private final HandlerRegistration handlerRegistration;
public BigPopup() {
setWidget(uiBinder.createAndBindUi(this));
handlerRegistration = Window.addResizeHandler(new ResizeHandler() {...});
...
}
@UiHandler("tstBtn")
void click(final ClickEvent event) {
handlerRegistration.removeHandler();
this.hide();
}
You should also make sure to disable the "Show Popup" button while the popup is displayed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With