title: Moving the visible area of a JScrollPane to a specific position
body: <p>I am trying to simulate the movement of a debugging cursor using java. I am having problem to get the viewable area of the JScrollPane to the right position.</p>
  
  <p>Here is a picture I want to achive:
  <a href="https://i.stack.imgur.com/9dMxG.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9dMxG.jpg" alt="The desired layout"></a></p>
  
  <p>I want to scroll only if the line I want to jump it is not visible. The calculation if it helps can by done using <code>CodeDrowingPanel.NUMBER_OF_LINES</code> and <code>CodeDrowingPanel.FONT_SIZE</code> the lines are painted on a panel using these constants.</p>
  
  <p>If I have to jump, the line I have to jump should be at the bottom.</p>
  
  <p>I have to bare in mind that the visible area depends of the screen resolution. The application is maximized with no chance of resizing.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <pre><code>public void setCursorToLine(int line, JScrollPane codeArea)
  {
      if(line*CodeDrowingPanel.FONT_SIZE &gt; this.getHeight()+43)
          this.cursorPosition = this.getHeight()+43;
      else
          this.cursorPosition = line * CodeDrowingPanel.FONT_SIZE;
      JViewport viewPort = (JViewport) SwingUtilities.getAncestorOfClass(JViewport.class, codeArea);
      if (viewPort != null) 
      {
          Rectangle view = viewPort.getViewRect();
          view.y += line - previousLine;
  
          codeArea.scrollRectToVisible(view);
      }
      this.repaint();
  }
  </code></pre>
  
  <p>This is how I am trying now to modify the line. But it does not work. I tried to follow your second example from the first comment. I don't know how to use the method from the second comment.</p>