I have a JScrollPane to which I am adding a JPanel. On this JPanel I paint components on the screen and then can click on them and drag them. I'm having difficulty getting this panel to scroll since it does not seem to work just by putting it in the scroll pane. Does my panel need to implement Scrollable or should I be looking at creating a custom scroll pane?
The scrollPane asks its child view component what size it needs for proper display. The panels layout manager checks with all of the panels child components and uses its constraints to lay them out and determines the preferred size needed for proper display. This is returned whenever the panel is asked by a parent container for its desired size. Since your JPanel has no components the queries from the scrollPane will get back the default JPanel size of (10, 10) or maybe even (0, 0). The solution is to either call setPreferredSize on or in the panel or override the getPreferredSize method in the panel and return the desired Dimension. The scrollPane will use this information to properly display the graphic component.
Thanks Craig. One of the other issues I'm having with that though is when I have a component that I drag above the (0,0) location. For example, imagine I have a square in the middle of the screen (no scrollbars yet) and I drag that to the top of the screen just enough to hide some of the square under the top of the scroll pane. At this point, even if I set my height correctly, the scroll bars would only provide a way to scroll down, not up to what I'm interested in. I could use scrollRectToVisible to show where I'm currently scrolling, but if I'm not actually dragging the component that moves out of view, this wouldn't work ... understand what I'm saying?
Originally posted by Jeff Storey: I have a JScrollPane to which I am adding a JPanel. On this JPanel I paint components on the screen and then can click on them and drag them.
You are painting things on the panel which the users can click/drag, but it's probably best not to call these Components because we use that term for special things that can be add()ed to the panel and know how to paint themselves.
I'm having difficulty getting this panel to scroll since it does not seem to work just by putting it in the scroll pane.
If the panel doesn't contain any Components (in the formal sense) then it will not have any size, and hence the scroll pane will not be able to scroll it. Try calling setPreferredSize() on the panel.
[edit: You guys are quick: a reply and a counter-reply in the time it took me to write that] [ December 13, 2006: Message edited by: Brian Cole ]
I appreciate the reply, but I'm still a little confused as to how I would handle the problem I stated in my "counter-reply." What happens what increasing the preferred size of the panel doesn't help because I've actually moved a painted object above 0 on the x-axis. Or, what if I have a group of these objects and I'm dragging one and this causes another to move off the top of the screen -- increasing the size of the panel probably would not help here since it would increase it down and to the right - still leaving my components off the top cut-off.
Originally posted by Jeff Storey: One of the other issues I'm having with that though is when I have a component that I drag above the (0,0) location.
I have seen this handled by immediately moving every object down and/or to the right so that they all have positive coordinates. It seems to me that it might be better to put in a translation layer between the non-negative panel coordinates and the coordinates of your objects.