开发者

Get the GWT ScrollPanel to start its vertical scrollbar at the bottom

开发者 https://www.devze.com 2023-02-25 00:58 出处:网络
I know there are some questions out there about the GWT ScrollPanel and how it works, but allow me to explain the situation.

I know there are some questions out there about the GWT ScrollPanel and how it works, but allow me to explain the situation.

I'm working on a project to implement QoS on routers. 开发者_如何学JAVAI'm now at the developping stage of the project and I need to make a webinterface to add protocols such as ssh and http and give them their bandwidth.

To save memory usage and network traffic, I do not use GWT-EXT or Smart GWT. So to set the bandwidths I use a ScrollPanel with an empty SimplePanel in it (which is way too big), leaving only the scrollbar.

Now here's the problem: I want each scrollbar for each added protocol to start at the bottom, not the top. I can get it working through the code if I manually move the scrollbar first, then any function works, like a scrollToBottom(), or a setScrollPosition(). If I want to move scrollbars through code before moving the scrollbar manually, however, I can't call a function on it.

(I would post a picture but I can't yet - new user)

Summary:

So if I add a protocol (using a button called btnAjouter), the two slidebars (One for guaranteed bandwidth and one for the maximum bandwidth) for each protocol start at the top. I want them to start at the bottom on the load of the widget. Is there a way to do this? Thanks in advance!

Glenn


Okay, my colleage found the solution. It's a rather dirty one, though. The thing is, the functions only work when the element in question is attached to the DOM. I did do a check with a Window.alert() to see if it was attached, and it was. But the prolem was that the functions were called to early, for example on a buttonclick it would've worked. The creation and attachment of the elements all happens very fast, so the Javascript can't keep up, this is the solution:

Timer t1 = new Timer()
        {
            @Override
            public void run() 
            {
                s1.getScroll().scrollToBottom();
                s2.getScroll().scrollToBottom();
            }
        };
t1.schedule(20);

Using a timer isn't the most clean solution around, but it works. s1 and s2 are my custom slidebars, getScroll() gets the ScrollPanel attached to it.


You can extend ScrollPanel and override the onLoad method. This method is called immediately after a widget becomes attached to the browser's document.

@Override
protected void onLoad() {
    scrollToBottom();
}


Could you attach a handler to listen to the add event and inside that handler do something like this:

panel.getElement().setScrollTop(panel.getElement().getScrollHeight());

"panel" is the panel that you add your protocol to. It doesn't have to be a ScrollPanel. An HTMLPanel will work.

You can wrap this method in a command and pass it to Schedule.scheduleDeferred if it needs to be called after the browser event loop returns:

Schedule.scheduleDeferred(new Scheduler.ScheduledCommand(
  public void execute() {
    panel.getElement().setScrollTop(panel.getElement().getScrollHeight());
  }
));

0

精彩评论

暂无评论...
验证码 换一张
取 消