Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

scrollPane

 
Rich Stepanski
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jefff willis
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean exactly?

Does it appear that your panel is not resized?

Or perhaps that your scroll bars don't appear?

Something else maybe?
 
Pat Hays
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rich

I think you should set the PreferredSize of your empty JPanel to be at least as big as the graphic you drawed is, if you have done so. Then you will see the ScrollBar properly.
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rich Stepanski
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic