Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Scrollbars wont appear on JPanel  RSS feed

 
Prasant Jagannath
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tryed just about every combination to get JScrollPane to work for me. I have created a JPanel and instantiated a ScrollPane with it, set preferred or default sizes, etc., but I cannot get a scroll pane on my image. Here is the code that I have, the main method is a simple instantiate and show from another class.

It gives me the picture fine, but no scroll bars! Note the hack that I attempted to add a JPanel that had a JScrollPane that had a JPanel. It doesnt work with just adding the JScrollPane to the contentpane
[ June 24, 2002: Message edited by: Prasant Jagannath ]
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ June 24, 2002: Message edited by: Dirk Schreckmann ]
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it should work if you add the JScrollPane directly to the content pane. your code looks ok. Are you extending JFrame or Frame? I suppose JFrame but check for that! I just had some strange behaviour in my GUI cause I forgot a J.
Maybe your image is just "not big enough"? The default is that JScrollPane doesn't show scroll bars if the viewport is totally visible. There is an option to change this behaviour. If you resize you frame to be smaller than the image, aren't the scrollbars showning up?
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try loading the image into an ImageIcon, adding the ImageIcon to a JLabel, and then adding the JLabel to the JScrollPane. What you were doing wasn't working because JPanel has a preferred size of 0x0... and you didn't override your JPanel's getPreferredSize() method to return the size of the image...
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!