Search code examples
javaswingjpaneljscrollpanepaintcomponent

Java JScrollPane Not Working as Expected


I have a JScrollPane with a JPanel inside of it. The JPanel is "zoomed" using the following code in the paintComponent method:

g2d.translate(this.getWidth()/2, this.getHeight()/2);
g2d.scale(scaleX, scaleY);
g2d.translate(-this.getWidth()/2, -this.getHeight()/2);

Now when I "zoom" in and the JPanel is scaled up to the point where it is larger than the JScrollPane I would expect the vertical and horizontal scroll bars to appear, however they do not. I tried to fix this by setting the preferred size of the JPanel to the size of the newly zoomed panel every single time the JPanel is "zoomed", but this doesn't do anything until the JFrame holding the JScrollPane is resized by the user (maybe this triggers a revalidate?). I was hoping someone could tell me what is going on or even a way to get the desired effect. Thanks!


Solution

  • I tried to fix this by setting the preferred size of the JPanel..

    Call revalidate() on the JScrollPane.