0

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.

Sarah
  • 145
  • 1
  • 14
  • Does the JScrollPane’s view (the child of its viewport) override the `getPreferredSize` method? If not, revalidating probably will have no effect. – VGR Feb 08 '17 at 19:33
  • 1
    What scares me is you seem to be using `static` references to various aspects of the your code, `static` is not a cross object communication mechanism and you should try and live without it (for this purpose). Without a runnable example of your problem, there is very little anybody can do for you – MadProgrammer Feb 08 '17 at 20:25
  • @VGR No, it doesn't. Should I go ahead and make one? – Sarah Feb 08 '17 at 21:32
  • @MadProgrammer Unfortunately, I can't post my entire code. It's about 9 classes right now, and copyrighted by my company. The above examples have been altered enough so that the company policy isn't violated. So I can't give much more than the above. But I can answer any questions you might have. – Sarah Feb 08 '17 at 21:34
  • Yes. The JScrollPane’s viewport will give your component its preferred size when performing layout. Obviously, that preferred size needs to be based on the size of the image. – VGR Feb 08 '17 at 21:34
  • @VGR ok, I'll give it a try. – Sarah Feb 08 '17 at 21:35
  • @Sarah I don't want your entire code, I want a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). [There's one possible example](http://stackoverflow.com/questions/12719085/how-to-zoom-in-to-jpanel-without-moving-the-center-math-or-swing/12719389#12719389), [there's probably a better one](http://stackoverflow.com/questions/27219914/how-to-increase-drawing-speed-of-jpicturebox-for-large-image/27220548#27220548) – MadProgrammer Feb 08 '17 at 21:38

0 Answers0