user26270
user26270

Reputation: 7074

GWT: how to get new size when doing onResize() in SplitLayoutPanel?

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?

Upvotes: 3

Views: 7467

Answers (1)

emerix
emerix

Reputation: 611

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.

Upvotes: 3

Related Questions