GWT:如何在SplitLayoutPanel中执行onResize()时获得新的大小? [英] GWT: how to get new size when doing onResize() in SplitLayoutPanel?

查看:97
本文介绍了GWT:如何在SplitLayoutPanel中执行onResize()时获得新的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SplitLayoutPanel,其中一个子组件是FlowPanel。我扩展了FlowPanel来创建一个ResizableFlowPanel,它实现了ProvideResize和RequiresResize,因为FlowPanel也有需要调整大小的子组件。

拖动SplitLayoutPanel的拆分器时,如何获取子组件的新大小?我已经尝试在FlowPanel中使用 getOffsetWidth() getElement()。getOffsetWidth ,并且两者都返回原来的大小,而不是新的大小。



我做错了什么?



这里是FlowPanel子类I' m试图:

  class ResizableFlowPanel扩展FlowPanel实现ProvideResize,RequiresResize {

ResizableFlowPanel(){
super();
}

public void onResize(){
int li_width = this.getOffsetWidth();
int li_height = this.getOffsetHeight();
System.out.println(ResizableFlowPanel :: width:+ li_width +; height:+ li_height);
for(Widget child:getChildren()){
if(child instanceof RequiresResize){
((RequiresResize)child).onResize();
}
}
}
}

I只是添加了System.out,以便在移动分离器时观察日志中的值,而不是使用断点。这就是我验证原始大小继续由getOffsetHeight()返回。



这也验证了对FlowPanel调用onResize()以及孩子的面板。我也尝试向FlowPanel添加一个边框,以查看拖动SplitLayoutPanel的分离器时是否发生了变化,并且边框没有改变。

所以这可以被重新设置为,为什么实现RequiresResize的SplitLayoutPanel的子项不会实际调整大小吗?或者为什么onResize()没有传入新的宽度和高度?

解决方案

我注意到,小部件可能需要一些时间。 (浏览器可能还没有完成所有的布局工作。)
所以你可能需要在你的onResize()方法中加入一个计时器:

  public void onResize(){
new Timer(){
@Override
public void run(){
int width = ResizeContainer.this.getOffsetWidth ();
}
} .schedule(100);
}

或使用延期调度程序:

  public void onResize(){
Scheduler.get()。scheduleDeferred(new Scheduler.ScheduledCommand(){
@Override
public void execute(){
int width = ResizeContainer.this.getOffsetWidth();
}
});
}

但是我在没有计时器或调度程序的情况下尝试了您的代码,可能是浏览器或gwt版本相关的问题...我使用GWT 2.1.1和Firefox 3.6.12。


I have a SplitLayoutPanel where one of the child components is a FlowPanel. I've extended FlowPanel to create a ResizableFlowPanel which implements ProvidesResize and RequiresResize, because the FlowPanel has child components which need to Resize as well.

When dragging the splitters of the SplitLayoutPanel, how do you get the new size of the child components? I've tried using getOffsetWidth() and getElement().getOffsetWidth from within the FlowPanel, and both are returning the original size, not the new size.

What am I doing wrong?

Here's the FlowPanel subclass I'm trying:

class ResizableFlowPanel extends FlowPanel implements ProvidesResize, RequiresResize {

    ResizableFlowPanel() {
        super();
    }

    public void onResize() {
        int li_width = this.getOffsetWidth();
        int li_height = this.getOffsetHeight();
        System.out.println("ResizableFlowPanel:: width : " + li_width + "; height: " + li_height);
        for (Widget child : getChildren()) {
            if (child instanceof RequiresResize) {
                ((RequiresResize) child).onResize();
            }
        }
    }
}

I just added the System.out to watch the values in the log as I moved the splitter, rather than using breakpoints. That's how I verified that the original size continued to be returned by getOffsetHeight().

This also verifies that the onResize() is being called, for the FlowPanel as well as the child panels. I also tried adding a border to the FlowPanel to see if that changes while dragging the SplitLayoutPanel's splitter, and the border does not change.

So this could be rephrased as, why doesn't a child of a SplitLayoutPanel which implements RequiresResize actually get resized? Or why doesn't onResize() pass in the new width and height?

解决方案

I noticed that getting the real width of a widget may take some time. (The browser may not have finished all its layout work yet.) So you may have to put a timer in your onResize() method :

    public void onResize() {
        new Timer() {
            @Override
            public void run() {
                int width = ResizeContainer.this.getOffsetWidth();
            }
        }.schedule(100);
    }

or use the deferred scheduler :

    public void onResize() {
            Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
            @Override
            public void execute() {
                int width = ResizeContainer.this.getOffsetWidth();
            }
        });
    }

But I tried your code without the timer or the scheduler and it worked so it may be a browser or gwt version related problem... I use GWT 2.1.1 and Firefox 3.6.12.

这篇关于GWT:如何在SplitLayoutPanel中执行onResize()时获得新的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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