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.
Here is a picture I want to achive:
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 CodeDrowingPanel.NUMBER_OF_LINES
and CodeDrowingPanel.FONT_SIZE
the lines are painted on a panel using these constants.
If I have to jump, the line I have to jump should be at the bottom.
I have to bare in mind that the visible area depends of the screen resolution. The application is maximized with no chance of resizing.
EDIT:
public void setCursorToLine(int line, JScrollPane codeArea)
{
if(line*CodeDrowingPanel.FONT_SIZE > 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();
}
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.