• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
  • Himai Minh
  • Carey Brown
  • salvin francis

Scrollbars wont appear on JPanel

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 ]
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?
Posts: 4121
IntelliJ IDE Spring Java
  • 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...
keep an eye out for scorpions and black widows. But the tiny ads are safe.
the value of filler advertising in 2020
    Bookmark Topic Watch Topic
  • New Topic