Q:

Java JPanel & JScrollPane

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);

A:

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.

