I have an empty JPanel inside a ScrollPane. I draw graphics on the JPanel that go out of what can be sean by the scrollPane. Regardless of how big I make the JPanel, I can't scroll the scrollPane to view it.
Question is - why can't I scroll around a blank panel? What do I need to do to be able to do so?
note: The verticalScrollBar is set to Always show. Thanks.
The JScrollPane will ask your custom (graphic) component for its size so it can figure out how to properly display it. If the component has child components then its layout manager will figure out a preferred size that the component can pass on to the JScrollPane when it calls to collect the information. When you render graphics/images on your component it has no way of knowing the dimensions of the graphics to pass on to the inquiring JScrollPane. So you can use the JComponent method setPreferredSize, as Pat Hays has said, to inform the JScrollPane. You also have the option of overriding getPreferredSize and returning the size requirements for your component.
This is very useful in the AWT where we don't have access to setPreferredSize.
Joined: Aug 24, 2004
Thanks a lot. Preferred size did it.
I have another odd problem though. When I scroll the panel now ,up or down, the panel's graphics get distorted. I have buttons above the panel(#1) in another panel(#2) and it seems the button graphics from #2 are scrolled on #1 like they were part of #1? if that makes sense.
Anyway I added AdjustmentListener to the vertical scrollbar and simply repaint when there is a change - this fixed that. I thought it was odd , does anyone else know a way around that? Thanks again.