Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JScrollPane doesn't work.

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there, I'm trying to get an image drawn from within a JPanels' paintComponent(Graphics g) method inside a JScrollPane.

The Drawer object (drawer) extends JPanel, and has size 800 by 800.
This has an image also 800 by 800.

The JFrame class is shown here:


But what happens is, I can only see top left corner of image, and there are no ScrollBars present to move the image around inside of the JScrollPane jsp object.

Can anyone help me?
Thanks
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you seem to be mixing the way to size:
null - setSize(..)
layoutManager - setPreferredSize(..)

(changed Drawer() to a JPanel with border)
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AMAZING, very fast too!!!

All I did was change drawer.setSize(...) to preferredSize(...)
And that worked

Don't know what the difference is, I'll look into it.

Thanks again!
 
T Eakins
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For those who came across this (as I did), wanted to post a follow-on solution. The code above works because there is no layout manager, which is uncommon for larger projects. Here's the same block using a common layout manager, MigLayout (not using any of its bells and whistles), and the SwingX implementation of JXPanel. Note that JXPanel can be easily replaced with JPanel, just remove the additional lines that tell the panel not to recalculate its size dynamically, but to enforce the scrollable region.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic