在GWT中将调整大小监听器/处理程序添加到VerticalPanel [英] Adding resize listener/handler to VerticalPanel in GWT

查看:109
本文介绍了在GWT中将调整大小监听器/处理程序添加到VerticalPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经基本尝试过了,当VerticalPanel发生变化时,触发
的触发器/处理函数。如下所示:

  VerticalPanel myPanel = new VerticalPanel(); 
// ...代码添加可能改变父垂直面板大小的子窗口小部件...

myPanel.addHandler(new ResizeHandler(){
public void onResize(final ResizeEvent event){
// ...处理垂直面板大小调整的代码...
}

},ResizeEvent.getType());

但处理程序不会被触发。

解决方案



 Window.addResizeHandler(new WindowResizeHandler())

调整大小处理程序将在用户调整浏览器窗口大小时调用:

  public void onResize(ResizeEvent event)

您可以使用 event.getHeight() event.getWidth()来获得更新后的窗口尺寸。



由于您只是为整个窗口获取事件,必须对您想要布置的组件进行自己的传播。我通常只是调用我自己的 doLayout()方法。这个方法甚至不需要resize事件的高度和宽度,因为DLH已经评论过了,当你得到通知时,你可以向大多数组件提出它们的尺寸,并且用你的计算。



不幸的是,有些浏览器不经常执行onResize。在试图不更新疯狂时,他们有时会持续几秒钟,让应用程序在等待正确布局时显得很难看。因此,在可能的情况下,您应该尝试将布局留给CSS,并且可能取决于您在表格与CSS战争中的表格。


How do I add a listener/handler to a VerticalPanel such that it is triggered when the VerticalPanel changes in size?

I've basically tried the following:

VerticalPanel myPanel = new VerticalPanel();
//... code to add child widgets that may change the size of the parent vertical panel ...

myPanel.addHandler(new ResizeHandler() {
  public void onResize(final ResizeEvent event) {
    //... code to handle resize of vertical panel ...
  }

}, ResizeEvent.getType() );

but the handler doesn't get triggered.

解决方案

What works reliably is the window-level resize listener:

Window.addResizeHandler(new WindowResizeHandler())

Your resize handler will be called when the user resizes the browser window:

public void onResize(ResizeEvent event)

You can use event.getHeight() and event.getWidth() to get the updated window dimensions.

Since you're only getting the event for the window as a whole, you'll have to do your own propagation to the components you want to lay out. I usually just call a doLayout() method of my own. This method doesn't even need the resize event's height and width because as DLH already comments, you can ask most components for their dimensions when you get notified, and do your calculations with that.

Unfortunately, some browsers don't do the onResize thing very often. In trying not to go crazy with updates, they may sometimes hold on to the event for some seconds, leaving your application visibly ugly while waiting for "proper" layout. Thus, where possible you should try to leave layout to CSS and possibly, depending on your stance in the "tables vs. CSS" war, tables.

这篇关于在GWT中将调整大小监听器/处理程序添加到VerticalPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆