Question

I have a JScrollPane. And I would like to know how much has been scrolled.
So if user is scrolling down, then I want to know in pixels or any other unit of measurement how far away the top is from what I am looking at.
The reason for that is I would like to do some drawing based on the amount scrolled, and in order for drawing to work I need to know the said amount to the top.

Was it helpful?

Solution 2

I am sorry, I have actually managed to answer my own question.
I created external variable called scrolledAmount, and then I have added listener to the scrollPane

    scrollPane_.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener()
    {
        @Override
        public void adjustmentValueChanged(AdjustmentEvent e)
        {
            scrolledAmount_ = e.getValue();
            System.out.println("Type is "+e.getAdjustmentType());
            System.out.println("Amount scrolled is "+e.getValue());
            System.out.println("Scrolled amount is "+scrolledAmount_);
        }
    });

By doing this I know the exact amount of pixels that has been scrolled.

OTHER TIPS

I would like to do some drawing based on the amount scrolled, and in order for drawing to work I need to know the said amount to the top.

You would probably use the viewport for this.

Point position = scrollPane.getViewport().getViewPosition();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top