Skip to main content

A bug in Component.scrollRectToVisible?

1 reply [Last post]
mikezang
Offline
Joined: 2009-06-26
Points: 0

I found a bug in class Component, please check it.

<br />
    protected void scrollRectToVisible(Rectangle rect, Component coordinateSpace) {<br />
        if (isScrollable()) {<br />
            int scrollPosition = getScrollY();</p>
<p>            Style s = getStyle();<br />
            int w = getWidth() - s.getPadding(isRTL(), LEFT) - s.getPadding(isRTL(), RIGHT);<br />
            int h = getHeight() - s.getPadding(false, TOP) - s.getPadding(false, BOTTOM);</p>
<p>            Rectangle view = new Rectangle(getScrollX(), getScrollY(), w, h);</p>
<p>            int relativeX = rect.getX();<br />
            int relativeY = rect.getY();</p>
<p>            // component needs to be in absolute coordinates...<br />
            Container parent = null;<br />
            if (coordinateSpace != null) {<br />
                parent = coordinateSpace.getParent();<br />
            }<br />
            if (parent == this) {<br />
                if (view.contains(rect)) {<br />
                    return;<br />
                }<br />
            } else {<br />
                while (parent != this) {<br />
                    // mostly a special case for list<br />
                    if (parent == null) {<br />
                        relativeX = rect.getX();<br />
                        relativeY = rect.getY();<br />
                        break;<br />
                    }<br />
                    relativeX += parent.getX();<br />
                    relativeY += parent.getY();<br />
                    parent = parent.getParent();<br />
                }<br />
                if (view.contains(relativeX, relativeY, rect.getSize().getWidth(), rect.getSize().getHeight())) {<br />
                    return;<br />
                }<br />
            }<br />
            if (isScrollableX()) {<br />
                if (getScrollX() > relativeX) {<br />
                    setScrollX(relativeX);<br />
                }<br />
                int rightX = relativeX + rect.getSize().getWidth();<br />
                if (getScrollX() + w < rightX) {<br />
                    setScrollX(getScrollX() + (rightX - (getScrollX() + w)));<br />
                } else {<br />
                    if (getScrollX() > relativeX) {<br />
                        setScrollX(relativeX);<br />
                    }<br />
                }<br />
            }</p>
<p>            if (isScrollableY()) {<br />
                if (getScrollY() > relativeY) {<br />
                    scrollPosition = relativeY;<br />
                }<br />

//**************************
// h is correct as below
<br />
                // int bottomY = relativeY + rect.getSize().getHeight();<br />
                int bottomY = relativeY + h;<br />

//*************************
<br />
                if (getScrollY() + h < bottomY) {<br />
                    scrollPosition = getScrollY() + (bottomY - (getScrollY() + h));<br />
                } else {<br />
                    if (getScrollY() > relativeY) {<br />
                        scrollPosition = relativeY;<br />
                    }<br />
                }<br />
                if (isSmoothScrolling()) {<br />
                    initialScrollY = getScrollY();<br />
                    destScrollY = scrollPosition;<br />
                    initScrollMotion();<br />
                } else {<br />
                    setScrollY(scrollPosition);<br />
                }<br />
            }<br />
            repaint();<br />
        } else {<br />
            //try to move parent scroll if you are not scrollable<br />
            Container parent = getParent();<br />
            if (parent != null) {<br />
                rect.setX(getAbsoluteX() - parent.getAbsoluteX() + rect.getX());<br />
                rect.setY(getAbsoluteY() - parent.getAbsoluteY() + rect.getY());<br />
                parent.scrollRectToVisible(rect, parent);<br />
            }<br />
        }<br />
    }</p>
<p>

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mikezang
Offline
Joined: 2009-06-26
Points: 0

After debug, I found the correct code as below
[code]
int bottomY = relativeY + rect.getSize().getHeight() - s.getPadding(false, TOP) - s.getPadding(false, BOTTOM);
[/code]