-1

I working on a application that manage image's filters etc.

I want to have scroll bars when the image is to big to be display. I put my customize panel that extend JPanel in a JScrollPane and I add it in my JFrame.

My image is displayed but not the whole image and the scroll bars are not there.

How to get the scroll-bars to appear?

Here is my code :

CustomePanel test = new ImagePanel(new File("test.jpg"));
test.setPreferredSize(new Dimension(400, 400));
JScrollPane tmp = new JScrollPane(test);
this.getContentPane().add(tmp);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Lenjyco
  • 41
  • 1
  • 1
  • 7
  • [`ImageViewer`](http://stackoverflow.com/a/13463684/418556) is a working example. If you cannot sort it out based on that code, post an [SSCCE](http://sscce.org/) of your best attempt. – Andrew Thompson May 01 '13 at 15:50
  • 1
    *"and I add it in my Frame"* DYM a `java.awt.Frame` or a `javax.swing.JFrame`? Don't mix Swing & AWT components without good reason. BTW - I added *"How to get the scroll-bars to appear?"* to the question, since it lacked one (a question, that is). If that is ***not*** your question, ask one of your own. – Andrew Thompson May 01 '13 at 15:52
  • my bad, indeed it is JFrame. – Lenjyco May 01 '13 at 15:57

2 Answers2

2

It is likely that your initial preferred size does not match that of your Image. Rather than using setPreferredSize, override getPreferredSize to reflect the size of the image in ImagePanel:

@Override
public Dimension getPreferredSize() {
   return new Dimension(image.getWidth(this), image.getHeight(this)); 
}

A JLabel would be a better approach here if the panel is not required as a container.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
0

try to set the same preferred size to scroll pane as well and test it

Sanjaya Liyanage
  • 4,706
  • 9
  • 36
  • 50