File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes slight problem using JScrollPane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "slight problem using JScrollPane" Watch "slight problem using JScrollPane" New topic
Author

slight problem using JScrollPane

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

i have an app that resizes the JFrame to fit the size of the image displayed in a JPanel. i decided to add a JScrollPane so it can display images that are larger than the screen(it cropped them). it works but the frame is a little too big and you cant see the horizontal scrollbar and only half of the vertical one. if i click the maximize button in the title bar, it displays correctly. how to correct this?

noticed i forgot to call super.paintComponent(g) but it worked anyway.
anyway adding that line had no effect.


SCJP
Visit my download page
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Can you us an SSCCE?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

sure. the entire program is very short.
i know. all my variables can be private. i changed that.
sorry if its a little hard to read, i'm still mixing tabs and spaces.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2202
    
    7
Not sure what pack() is doing when the preferred size is greater than the screen size, but on my system the horizontal/vertical sizes where both 12 pixels greater than the max.

I used the following to customize the size:



Also, there is no need to use a custom panel to display the image. I just added a JLabel to the scrollpane and then used:

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

ok. thanks Rob. the first code you posted looks like what i need. i used pack because sometimes the image is small. your second code is interesting as well.

that fixed it thanks
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

FYI for anyone who cares. i tried Rob's suggestion about not needing to call repaint(). it worked at first(when files were different size). but when i opened files the same size(my documents/my pictures/sample pictures), pack() didn't call repaint(). in fact pack didn't get called because the size was the same. so i needed to call repaint() after all.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2202
    
    7
If you use a JLabel and reset the Icon there is no need to call repaint() because the setIcon() method calls repaint().
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

i'll try that next since it will be less code than using custom JPanel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: slight problem using JScrollPane