Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 615
Anoyz Avatar asked Feb 21 '23 15:02

Anoyz


1 Answers

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.

like image 65
trashgod Avatar answered Mar 15 '23 16:03

trashgod