JDK-6333318 : JViewPort.scrollRectToVisible( Rectangle cr ) doesn't scroll if cr left or above
  • Type: Enhancement
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 5.0
  • Priority: P5
  • Status: Open
  • Resolution: Unresolved
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2005-10-06
  • Updated: 2013-05-15
Description
A DESCRIPTION OF THE REQUEST :
The JViewPort.positionAdjustment when called from JViewPort.scrollRectToVisible( ) doesn't account for the content being to the left or above the JViewPort's rectangle. It doesn't compute a delta value ( != 0 )  in this case causing the JViewPort.scrollRectToVisible( ) to do nothing. Requesting the logic be altered so that if my content rectangle is to left of above that it computes a non-zero delta value and scrolls the viewport.

JUSTIFICATION :
The end programmer shouldn't have to worry about the location of his rectangle only if it is somewhere outside the JViewPort's current rectangle ( JViewPort.getViewRect() ).

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I would like the JViewPort.scrollRectToVisible to move the viewport.
ACTUAL -
The viewport is not moved when the content rectangle falls to the left or above the viewport's current rectangle.

---------- BEGIN SOURCE ----------
viewport's rectangle:
   x=0
   y=512
   width=846
   height=604

content rectangle:
   x=409
   y=505
   width=12
   height=23


The following method will return a delta of 0 on the first 'if' when the prior identified viewport and content rectangles are used:

    private int positionAdjustment(int parentWidth, int childWidth, int childAt)
    {

        //   +-----+
        //   | --- |     No Change
        //   +-----+
        if (childAt >= 0 && childWidth + childAt <= parentWidth)
        {
            return 0;
        }
        .......
     }

As you can see the content's rectangle falls outside the bounds of the viewport's rectangle because a zero delta value is returned from the method the following if inside JViewPort.scrollRectToVisible() leaves without scrolling.

            if (dx != 0 || dy != 0)
            {
                 .......
            }


---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
   public void scrollRectToVisible( Rectangle contentRectangle )
   {
      // I had to right all this code because JViewPort's 'scrollRectToVisible()'
      // appears to have a bug. It won't scroll for you if your content area
      // ( rectangle ) is above or to the left of the viewport's rectangle??
      Rectangle viewportRectangle = scrollPane.getViewport().getViewRect();
      if ( !viewportRectangle.contains( contentRectangle ) )
      {
         int outCode = viewportRectangle.outcode( contentRectangle.getLocation() );
         if ( ( outCode & Rectangle2D.OUT_LEFT ) > 0 || ( outCode & Rectangle2D.OUT_TOP ) > 0 )
            scrollPane.getViewport().setViewPosition( contentRectangle.getLocation() );
         else
         {
            outCode = viewportRectangle.outcode( new Point( contentRectangle.x + contentRectangle.width, contentRectangle.y + contentRectangle.height ) );
            if ( ( outCode & Rectangle2D.OUT_RIGHT ) > 0 || ( outCode & Rectangle2D.OUT_BOTTOM ) > 0 )
               scrollPane.getViewport().scrollRectToVisible( contentRectangle );
         }
      }
   }