JDK-5049000 : java.awt.ScrollPane.getScrollPosition works wrong sometimes
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 5.0
  • Priority: P3
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: solaris_2.6
  • CPU: sparc
  • Submitted: 2004-05-18
  • Updated: 2006-11-02
  • Resolved: 2006-11-02
Related Reports
Relates :  
Relates :  
Relates :  
Description
Name: sdR10048			Date: 05/18/2004


Filed By      : SPB JCK team (###@###.###)
JDK           : build 1.5.0-beta2-b51
Platform[s]   : Solaris
Specification excerpt:
======================
--------- J2SE API spec v.1.5 ---------
...
java.awt.ScrollPane:
==================
public void setScrollPosition(Point p)
Scrolls to the specified position within the child component. A call to this method is only valid if the scroll pane contains a child and the specified position is within legal scrolling bounds of the child. Specifying a position outside of the legal scrolling bounds of the child will scroll to the closest legal position. Legal bounds are defined to be the rectangle: x = 0, y = 0, width = (child width - view port width), height = (child height - view port height). This is a convenience method which interfaces with the Adjustable objects which represent the state of the scrollbars. 

Parameters:
    p - the Point representing the position to scroll to

java.awt.ScrollPane:
==================
public Point getScrollPosition()
Returns the current x,y position within the child which is displayed at the 0,0 location of the scrolled panel's view port. This is a convenience method which interfaces with the adjustable objects which represent the state of the scrollbars. 

Returns:
    the coordinate position for the current scroll position 
Throws: 
   NullPointerException - if the scrollpane does not contain a child

java.awt.Toolkit
==============
Many GUI operations may be performed asynchronously. This means that if you set the state of a component, and then immediately query the state, the returned value may not yet reflect the requested change. This includes, but is not limited to: 

    Scrolling to a specified position. 
    For example, calling ScrollPane.setScrollPosition and then getScrollPosition may return an     incorrect value if the original request has not yet been processed.

    ...
---------- end-of-excerpt ---------------

Problem description
===================
Sometimes the method

    java.awt.ScrollPane.getScrollPosition()

returns the wrong position, not just the one that has been set before.

"Sometimes" means if we call setScrollPosition multiple times in rapid succession.
Please see the demo.
Note that this is an intermittently error -- thus may be you'll have to run this demo 
several times to reproduce it. Please use the Solaris platform, I couldn't reproduce
it under the Linux.

May be it will be interesting: please note the output of the getScrollPosition() in the demo's output: java.awt.Point[x=12,y=11]
Not java.awt.Point[x=11,y=11] or java.awt.Point[x=10,y=10]...

Minimized test:
===============
------- J.java -------
import java.awt.*;

public class J {
    public static void main(String[] args) {

        // some GUI preparatory stuff
        Frame frame = new Frame();
        frame.setBounds(100, 100, 300, 300);
        ScrollPane sp = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
        Canvas canvas = new Canvas();
        canvas.setSize(500, 500);
        sp.add(canvas);
        frame.add("Center", sp);
        frame.setVisible(true);

        // the test itself
        for (int i=1; i<100; i++) {
            Point p = new Point(i, i);
            sp.setScrollPosition(p);

            // waiting till the next scroll position "arrives"
            while ( !p.equals(sp.getScrollPosition()) ) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.exit(1);
                }
                System.out.println("set position : "+p);
                System.out.println("get position : "+sp.getScrollPosition());
                System.out.println("waiting...");
            }
        }
        frame.dispose();
        System.out.println("PASSED");
    }
}

------- end-of-J.java -------

Minimized test output:
======================
] (18:06:17) dsv@orion ~/tmp
] java -showversion J
java version "1.5.0-beta2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b51)
Java HotSpot(TM) Server VM (build 1.5.0-beta2-b51, mixed mode)

set position : java.awt.Point[x=12,y=12]
get position : java.awt.Point[x=12,y=11]
waiting...
set position : java.awt.Point[x=12,y=12]
get position : java.awt.Point[x=12,y=11]
waiting...
set position : java.awt.Point[x=12,y=12]
get position : java.awt.Point[x=12,y=11]
waiting...
set position : java.awt.Point[x=12,y=12]
get position : java.awt.Point[x=12,y=11]
waiting...
^C

======================================================================

Comments
EVALUATION I'm not able to reproduce it with JDK6.0 with XAWT option on Linux (GNOME and KDE), Solaris 9 CDE. I tried JDK versions from 1.4 up to JDK6.0rc. Only two times I noticed that defect. Both with MToolkit. First one with JDK1.4.1 and second with JDK6.0b59d with the following output: <name@host(pts/2).276> AWT_TOOLKIT=MToolkit [path]/6.0/promoted/beta/b59d/binaries/linux-i586/bin/java J set position : java.awt.Point[x=9,y=9] get position : java.awt.Point[x=1,y=9] waiting... set position : java.awt.Point[x=9,y=9] get position : java.awt.Point[x=1,y=9] waiting... ... [Much more the same]
23-10-2006

EVALUATION Attached slightly changed test J.java, it changes scrolling position slightly back and forth. In addition, it has ScrollPane.SCROLLBARS_NEVER set. Thus modified test fails half of the time on Windows (to be more specific, on Windows XP with XP theme) with jdk6. That's the same behavior as in 6466675 with 6404832 fix applied.
07-09-2006

EVALUATION Name: adR10249 Date: 05/19/2004 This is a consequence of #4075484. There is a new invocation of native method ScrollPaneAdjustable.setTypedValue() added in src/solaris/classes/sun/awt/motif/MScrollPanePeer.java . As a result, some kind of thread race occured in awt: an event from the native system may arrive after the next setScrollPosition(x,y) call by user (as in example). ###@###.### 19-May-2004 ======================================================================
17-09-2004