File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JScrollPane doesn't work. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JScrollPane doesn Watch "JScrollPane doesn New topic

JScrollPane doesn't work.

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 750
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?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you seem to be mixing the way to size:
null - setSize(..)
layoutManager - setPreferredSize(..)

(changed Drawer() to a JPanel with border)
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 750
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

Joined: Feb 20, 2004
Posts: 9
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.

I agree. Here's the link:
subject: JScrollPane doesn't work.
It's not a secret anymore!