How do scrollbars work?

I need to know how scrollbars work because the Built in java scrollbars aren’t good enough for what I need. I need to know how to calculate the (ratio?) of the viewing area and when you drag the bar, how does it change the view? Any custom scrollbar examples?

Thanks.

It’s pretty simple.

You need to know:

  1. How big your viewing area is.
  2. How big your content within the viewing area is.
  3. How long the scrollbar pane is (not the slidey part, but the place that can get slid around in).
  4. What position the content is currently at.

Then it’s all a matter of simple ratio calculation.

Let’s just do this with height for now.

ratio = view.height / content.height
scroller.height = scrollpane.height * ratio
scrollRatio = scroller.y / scrollpane.height
content.y = scrollRatio * content.height

And voila.

You may also be using the scrollbars incorrectly. What are you trying to do? I have used them before and they worked for me.

I’m not using JScrollBar, I’m implementing my own

Not quite, you forgot a tiny little thing:

ratio = view.height / content.height scroller.height = scrollpane.height * ratio scrollRatio = scroller.y / [u](scrollpane.height - scroller.height)[/u] content.y = scrollRatio * content.height

Yeah you’re right. I just threw that up there quickly. Plus I said “4) What position the content is currently at.” but really I meant “4) What position the scrollbar is currently at.”

But you might be able to use JScrollPane. You said the built in ones are not good enough, so I am asking what you are trying to do to see if you can use the built in ones.