Roughian Examples Site Map - Examples - Tutorials

ScrollListener


Version 1.0 onwards

Tells You When A ScrollPanel Scrolls



Sources



Notes


ScrollPanels scroll and ScrollListeners listen to ScrollPanels scrolling. The only time I have needed to use it, was when writing a Chat application. It the message list was scrolled to the bottom, I wanted the scroll panel to scroll to display the latest message as it came in. If the user has scrolled away from the bottom of the list, then presumably they were reading a message further up the list, so I didn't want to scroll to the latest message but stick where we were.


Code


import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollListener;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.roughian.rxf.client.ui.AbstractRemotePage;
import com.roughian.rxf.client.ui.RootPanelUncached;

public class Listeners__ScrollListener extends AbstractRemotePage implements ScrollListener
{
    Label ind = new Label("Scroll The Panel For The Demo");
    public void buildPage()
    {
        ScrollPanel panel = new ScrollPanel();
        panel.addScrollListener(this);
        panel.setHeight("200px");
        DOM.setStyleAttribute(panel.getElement(), "border", "1px solid #000");
        DOM.setStyleAttribute(panel.getElement(), "borderBottom", "0");
        RootPanelUncached.get("demo").add(panel);

        RootPanelUncached.get("demo").add(ind);
        DOM.setStyleAttribute(ind.getElement(), "textAlign", "center");
        DOM.setStyleAttribute(ind.getElement(), "backgroundColor", "#ddd");
        DOM.setStyleAttribute(ind.getElement(), "border", "1px solid #000");

        VerticalPanel content = new VerticalPanel();
        panel.add(content);

        for (int i = 0; i < 100; i++)
        {
            content.add(new Label("Line " + (i + 1)));
        }

    }
    public void onScroll(Widget widget, int scrollLeft, int scrollTop)
    {
        ScrollPanel sp = (ScrollPanel) widget;
        ind.setText("ScrollTop=" + scrollTop);
    }
}