Search code examples
javaswingscrollbarjscrollpanejscrollbar

Get scrolled amount


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.


Solution

  • 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.