| Author |
slight problem using JScrollPane
|
Randall Twede
Ranch Hand
Joined: Oct 21, 2000
Posts: 4095
|
|
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
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Can you us an SSCCE?
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Randall Twede
Ranch Hand
Joined: Oct 21, 2000
Posts: 4095
|
|
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: 1791
|
|
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: 4095
|
|
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: 4095
|
|
|
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: 1791
|
|
|
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: 4095
|
|
|
i'll try that next since it will be less code than using custom JPanel
|
 |
 |
|
|
subject: slight problem using JScrollPane
|
|
|