2

I realize that there's another question relating to "infinite" JScrollPanes, however I think that what I'm looking for is something that is subtly different.

Basically, I have a collection of objects which can be dragged in any direction, and the extent of the scrolling viewport should always encompass the bounding rect of all those objects. To put it another way, the document has no fixed "origin". Thus, if you drag an object to the left, off the edge of the screen, then the viewport extent should expand in the negative direction to encompass that object's new position. (It should also auto-scroll as you drag, but that's a separate problem I realize.)

I'm not sure how to do this using the JScrollPane API, which seems to want you to set the preferred size of the underlying component, but doesn't seem to have the concept of an offset or origin for that component. (The underlying JViewport seems like it would be able to do it, but I can't really figure out the JViewport API.)

Talin
  • 1,397
  • 2
  • 15
  • 30
  • Since you brought it up - that is actually a fairly accurate measurement of the number of questions I've asked where I wasn't satisfied with any of the answers. That's not a whinge, just an observation. – Talin Jan 08 '13 at 17:11

2 Answers2

4

The scroll pane and view port actually have nothing to do with it. What you need to is change the preferred size of the view ports view and let the scroll pane/view port take care of this rest.

Basically, what you need to do, is calculate the widest and highest points on your component and invalidate the view port, to force to recalculate it's layout requirements.

Create yourself a custom component (using a JPanel for example) and override the getPreferredSize method. This method should return the required size of your component.

When the size requirements change, call revalidate()

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • @Talin this issue were solved here a few times, search for zoom_in/out in JScrollPane, (to Mad not invalitade [description about JScrollPane by Kleopatra](http://stackoverflow.com/a/13974131/714968)) – mKorbel Jan 08 '13 at 09:17
  • @mKorbel your probably right, I think up could call invalidate on the view component and validate on the viewport - got to get the parent to update the layout some how – MadProgrammer Jan 08 '13 at 09:22
  • :-) I'm hate `invalidate()`, nothing else, this is reason for my commmmmmmmmm – mKorbel Jan 08 '13 at 09:28
  • 1
    @MadProgrammer just calling `revalidate()` on the "changed" component should be enough. – Guillaume Polet Jan 08 '13 at 09:30
  • I still don't understand how to get the viewport to expand in the negative direction. In other words, if I drag an object to coordinates (-100, -100), how do tell the scroll pane to make that object visible? Changing the preferred size only affects how far I can scroll to the right/down, not left/up. I would need four numbers to describe the scrolling bounds, and preferred size is only two numbers. – Talin Jan 08 '13 at 17:02
  • 1
    @talin you can't. Of you need this kind feature, then your going to have to setup a virtual world that is capable of converting the virtual coordinates back to physical ones, or you need to translate the negative position back to a positive position (say 0x0 for arguments sake) and "move" all the other objects accordingly to maintain there relative positions – MadProgrammer Jan 08 '13 at 19:41
1

OK so it turns out that the simple answer is that scroll panes cannot scroll to negative coordinates. What you have to do, as @MadProgrammer suggested, is maintain a separate offset coordinate which is a Point. The offset stores the top/left coordinates of the entire document. When rendering individual objects, set the transform of the Graphics2D object such that they are shifted down and to the right by the offset amount, so that all object are drawn at coordinates that are positive numbers even though in reality they may be located at negative coordinates. Similarly, when calculating the preferredSize for the scroll pane, add in the offset to the document width and height. Coordinates from scroll bar events also need to be offset as appropriate.

This allows you to maintain the illusion that the document bounds are not constrained to be positive numbers, and the document's boundary can expand infinitely in any direction.

Talin
  • 1,397
  • 2
  • 15
  • 30