2

I have read an image into a JLabel and scaled the image to the size of the JLabel like this:

File file = new File ("C:/Users/Me/Desktop/TestImages/test.jpg");
BufferedImage image = ImageIO.read(file);

Image scaledImage = image.getScaledInstance(label.getWidth(), label.getHeight(), Image.SCALE_SMOOTH);
label.setIcon(new ImageIcon(scaledImage));

I'm using the provided GroupLayout and set the horizontal and vertical axes to "Auto-Resizable" (via Design view) so that the size of the JLabel auto-adjusts to the size of the JFrame whenever I change the window size.

Now I'd like to adjust the size of the image together with the size of the JLabel and here starts the trouble. I have added an event handler that responds to resizing the JFrame like this:

addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                addScaledImage(); // executes the lines above 
            }
        });

This works in theory but is extremely slow. So I tried adding the event handler to the JLabel instead of the JFrame but that leads to another issue: the image keeps expanding without any action being performed. Then I surrounded the JLabel with a JScrollPane and added the event handler to the JScrollPane. This works as long as I'm expanding the size of the JFrame. However, when I reduce the windows size, the image is not being reduced in size but instead stays the same.

  1. Is there a more elegant way to integrate the event handler (maybe without help of a JScrollPane)?

  2. How can I make my image/JLabel auto-resize together with any resizing event (increasing and decreasing the size of the JFrame)? Is another approach (e.g. another LayoutManager) maybe better suited for this?

  3. Also, if I'd like to keep the height-to-width ratio of my image, is there a scaling method designated to this?

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Possible duplicate [Scale the ImageIcon automatically to label size](https://stackoverflow.com/questions/14548808/scale-the-imageicon-automatically-to-label-size/14553003#14553003) – MadProgrammer Aug 14 '18 at 20:11
  • 1
    The second half of [this answer](https://stackoverflow.com/questions/11698271/how-to-make-jlabel-with-image-fill-borderlayout-center/11698479#11698479) also provides a slightly more optimised scaling approach - it reduces the number of times that a scaling operation is executed - which is expensive – MadProgrammer Aug 14 '18 at 20:15
  • *"However, when I reduce the windows size, the image is not being reduced in size but instead stays the same"* - Okay, any number of possible issues, but most likely, the `JScrollPane` is not been reduce in size, even you weren't using one, the `preferredSize` of the `JLabel` may be preventing the layout manager from "squeezing" the component – MadProgrammer Aug 14 '18 at 20:22
  • Hi MadProgrammer. Thanks for providing some links and helping me to understand the issue a little better! –  Aug 15 '18 at 16:16

1 Answers1

2

I'd like to adjust the size of the image together with the size of the JLabel

You can use the Stretch Icon. It will scale automatically when the label size is changed. Works for any components that can display an Icon.

Then I surrounded the JLabel with a JScrollPane

However, when I reduce the windows size, the image is not being reduced in size but instead stays the same

You should not be using a JScrollPane. The size of the component added to the scroll pane doesn't change when the scroll pane shrinks. Instead scrollbars are displayed.

I'm using the provided GroupLayout and set the horizontal and vertical axes to "Auto-Resizable

I would not use an IDE to generate the layout code and I would not use GroupLayout as it is overly complex for something this simple.

Just add the label (using the StretchIcon) to the frame. By default the frame uses a BorderLayout which will automatically resize any component added to the CENTER of the BorderLayout.

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288