4

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.

Anoyz
  • 7,431
  • 3
  • 30
  • 35

3 Answers3

3

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • That's not exactly what I asked. I do not want to scroll to a specific content. I have a very large content, and I want my scrollbar to just consider a small portion of that content. That is, I want to define the scrollbar bounds, such that when the scrollbar knob is at most left (or right), the shown content is a specific part of the content. – Anoyz Feb 01 '12 at 14:55
  • Ah, now @IaD's answer makes sense; I'm not aware of anything better. In general, it's going to be easier to solve this in the data model rather than the view. Is the content well encapsulated, e.g. `TableModel`? – trashgod Feb 01 '12 at 16:43
  • The content is rather complex: several JPanels, some with text, others with many components. So there is not single data model. As laD said bellow, a possible answer would be trimming the contents to show only the part that should be visible in the viewPort. However, since the contents are so complex, this envolves many (unplanned) changes. I was looking for some solution that would still allow me having all the content, but only consider part of it for the scrolling. – Anoyz Feb 01 '12 at 17:16
  • `CardLayout` may be your best option for controlling visibility; more above. – trashgod Feb 01 '12 at 17:31
  • Thanks for the help. I just figured it out: the scrollbars' model has a strict policy of: minimum <= value <= value+extent <= maximum And that's why changing the minimum and maximum values and only then changing the "Value" value was not working. Fortunatelly, using the "setValues" method, I managed to set all the variables at once. scrollBar.setValues(newValue, 1, minimum, maximum); This allows me to scroll ONLY between [minimum] and [maximum] though the content is much larger. – Anoyz Feb 02 '12 at 13:15
  • I hadn't thought to use `setValues()`; ping me if you add this approach as an answer. – trashgod Feb 02 '12 at 13:38
  • 1
    Since I found no other solution, I made an answer with that approach. Thank you for your time and help :) – Anoyz Feb 06 '12 at 13:33
3

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.

Anoyz
  • 7,431
  • 3
  • 30
  • 35
2

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.

IaD
  • 141
  • 2
  • I could indeed try to trim the content, instead of fighting the normal behaviour. However, this is a small part of a big platform, and trimming the contents would require a lot of (unplanned) changes. It is my last resort, if I can't find any other solution. – Anoyz Jan 31 '12 at 20:13