aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Scrolling Components Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Scrolling Components" Watch "Scrolling Components" New topic
Author

Scrolling Components

Jeff Storey
Ranch Hand

Joined: Oct 09, 2006
Posts: 118
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?

Thanks.
Jeff
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
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.
Jeff Storey
Ranch Hand

Joined: Oct 09, 2006
Posts: 118
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?
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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 ]

bitguru blog
Jeff Storey
Ranch Hand

Joined: Oct 09, 2006
Posts: 118
Hi Brian,

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.

Thanks.
Jeff
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Seems like you will need to find a way to dynamically resize your graphic component
during or shortly after your drag activity. Here's a suggestion.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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.
 
jQuery in Action, 2nd edition
 
subject: Scrolling Components