Anoyz
Anoyz

Reputation: 7691

Make java scrollbar start in middle of the scrolling content

I have a horizontal scrollbar that controls a large panel (with a very large width, and a very small height, thus an horizontal panel).

I want the start of the scrollbar (when the knob is at max left) NOT to start at the beggining of the panel it is scrolling, but rather in a specific place that I dictate. The same for the end of the scrollbar (when the knob is at max right).

I find that the scrollbar is always bound to the panel it is scrolling, and I can't figure out how to change its behaviour.

EDIT:

As an example, picture a normal web-page: when at the top of the page, the scrollbar knob is also at the top. When at the bottom, the scrollbar knob is at the bottom. I want to define new limits for the content, such that when the scrollbar knob reaches the top or bottom, the page is showing the limit I defined, instead of the real top and bottom.

Upvotes: 4

Views: 2400

Answers (3)

Anoyz
Anoyz

Reputation: 7691

I solved the problem by using the JScrollbar method setValues(), which allows me to set at the same time the maximum, minimum, value and extent of the scrollbar. By setting the maximum and minimum to the values I want, the scrollbar behaves as I wanted/expected.

The problem was that I was only setting maximum and minimum values (setMaximum, setMinimum), and since there is a strict policy at the model that minimum <= value <= value+extent <= maximum, that estrategy did not work.

Upvotes: 3

trashgod
trashgod

Reputation: 205795

As shown in How to Use Scroll Panes, you can use the component's scrollRectToVisible() method to scroll to an arbitrary Rectangle. There's an example here.

Addendum: As a Container, a JPanel is fairly fungible even if it has considerable nested content. One reliable way to swap content at a given level is via CardLayout, shown here, here and here.

Upvotes: 3

IaD
IaD

Reputation: 141

Would it be possible to keep the large panel as a backing store and copy the region of interest into a panel which is actually realized in the scrollpane. This way you don't have to fight the behavior of the scrollpane.

Upvotes: 2

Related Questions