JViewPort position

I have a JTextPane set in a JScrollPane. I need to know when my client has scrolled all the way to the bottom of the TextPane so I can execute some other function. What I have been trying to do is compare the current position of the scroll bar knob and compare it to the view port size. I get the current position of the knob and the view port size like this:

Point p = scrollPane.getViewport().getViewPosition();

Dimension h = scrollPane.getViewport().getViewSize();

As you can see I have a dimension and a point. Now, when I compare h.getHeight() and p.getY() I never get the correct answer. How can I solve this problem? How can I know when I am at the bottom of the viewport. I don't seem to be understanding the concept of the point and the dimension. Please, someone help me with this. I have been stuck here for a whole day now.

Thanks!!

[876 byte] By [lgarcia3a] at [2007-10-2 7:33:20]
# 1

> Point p = scrollPane.getViewport().getViewPosition();

This gives you the coordinates of the top left position of the viewport

> Dimension h = scrollPane.getViewport().getViewSize();

This gives you the height of the viewport.

So p.y + h.height gives you the "relative" size of the viewport. If this value is equal(?) to the scrollPane height, then I would say you are at the bottom of the scrollPane.

Another option might be to use getValue() from the vertical scrollbar and compare it to the maximum value of the scrollbar.

Either way, create a simple example program and display the values of each of the methods to see how they change as the scrollbar is moved. That way if you still have a problem you can post your test code for us to look at.

camickra at 2007-7-16 21:13:36 > top of Java-index,Desktop,Core GUI APIs...
# 2

Well, I guess that writing the problem I have kind of gave me the answer as well. If someone is interested, the solution is to substract the height of the viewPortSize() minus the height of the getExtentSize() and that gives you the bottom of the viewport no matter if you resize the window or not.

So:

Point p = scrollPane.getViewport().getViewPosition();

Dimension h = scrollPane.getViewport().getViewSize();

Dimension exT = scrollPane.getViewport().getExtentSize();

if(p.getY() == (h.getHeight()-exT.getHeight())){

System.out.print("I'm at the BOTTOM\n");

return;

}

if(p.getY() == 0){

System.out.print("I'm at the TOP\n");

return;

}

And that is it!!

lgarcia3a at 2007-7-16 21:13:36 > top of Java-index,Desktop,Core GUI APIs...