I have a JScrollPane attached to a JPanel all contained in a JFrame. I also have a custom slider class that extends JSlider. It's also contained in the JFrame. My issue is, that as the JSlider zooms in on the image in my panel, my JScrollPane isn't resizing with the image zoom. I've tried everything from resetting the bounds on the JScrollPane to manually adjusting the scrollbar to resetting the preferred size. Nothing I've seen on other stackoverflow questions seems to be working for me. Here's some of my code where I add the scroll pane as it currently is (sans a few superfluous details):
JFrame frame = new JFrame("");
frame.setTitle("Image Viewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
if (img.getWidth() > START_WIDTH || img.getHeight() > START_HEIGHT) {
frame.setSize(START_WIDTH, START_HEIGHT);//variables set elsewhere in the code
} else {
frame.setSize(img.getWidth(), img.getHeight());//img is the BufferedImage displayed in the JPanel
}
parentPane = new BackgroundPane(img, frame.getWidth(), frame.getHeight()); //BackgroundPane is the JPanel. More accurately, it's a custom class extending JPanel.
scroll = new ImageScroller(parentPane,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS, img); //Custom class extending JScrollPane. This is the problematic area. Its size needs to readjust. It's also a class variable
frame.setLocationRelativeTo(null);
ImageSlider slider = new ImageSlider(SelectionRectangle.this); //the zoom bar extending JSlider
{...}//some removed superfluous code
frame.add(slider, BorderLayout.SOUTH);
frame.add(scroll);
frame.pack();
frame.setVisible(true);
And here is the code currently being used in the ImageSlider for when the panel zooms. Most of my attempts to readjust the scroll bar have occurred here:
@Override
public void stateChanged(ChangeEvent arg0) {
int value = ((JSlider) arg0.getSource()).getValue();
SCALE = value / 100.0; //Scaling factor
ACTIVE_FRAME.revalidateViewport(); //Method within the frame class. See below
}
RevalidateViewport function:
public void revalidateViewport() {
scroll.getViewport().revalidate();
scroll.getViewport().repaint();
}
How can I get the scrollbar to resize based on my scale factor? Is there some simple way I just haven't seen yet? I'm new to JScrollPanes.
Thank you. Let me know if you have any questions.