1

I need to develop an image gallery of sorts, where I can display images in a JFrame, and I can scroll and double click to expand to full screen etc. I was able to display the images the way I want it, but when I resize the JFrame, the images gets cut off as there is no horizontal scroll.

Is there a way where I can have have vertical scroll and when frame resizes, the panel within will resize to fit horizontally?

try {
    
    BufferedImage readImage = ImageIO.read(new File("myFile.png"));
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ImageIO.write(readImage, "png", bos);
    byte[] data = bos.toByteArray();
    BufferedImage image = ImageIO.read(bis);
    
    JPanel imagePanel = new JPanel();
    imagePanel.setLayout(new GridLayout(0,2,5,5));
    for (int x=0;x<8;++x){
        JLabel l = new JLabel(new ImageIcon(image));
        imagePanel.add(l);
    }
    
    JScrollPane scrollPane = new JScrollPane(mainPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    
    JFrame frame = new JFrame();
    frame.add(scrollPane);
    //frame.add(imagePanel); //This will work as i intended it to be, but there is no vertical scroll
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
} catch (IOException e) {
    e.printStackTrace();
}

Edit:

A visual example is shown below. When the frame resizes, the images get cropped, what I would like instead are for the images within the JScrollPane to resize so that it could all fit horizontally, while at the same time maintaining the vertical scroll.

cropped images

Edit2:

Using StretchIcon, I was able to achieve having the images resize dynamically, but this introduced another issue where there are huge spaces in between the images as I resize.

large gaps

Cherple
  • 725
  • 3
  • 10
  • 24
  • 1
    Not really sure I understand the question but maybe you can: 1) use the [Stretch Icon](https://tips4java.wordpress.com/2012/03/31/stretch-icon/). I Icon will automaticallylresize based on the space available and 2) use the [Scrollable Panel](https://tips4java.wordpress.com/2012/03/31/stretch-icon/) It allows you to fix the width of the panel to the width of the viewport of the scroll pane. – camickr Oct 17 '22 at 17:36
  • @camickr I've edited my question with images to illustrate what the issue I am facing – Cherple Oct 18 '22 at 01:42
  • More or less what I thought so it doesn't change my answer. – camickr Oct 18 '22 at 01:54
  • @camickr, Thank you for the answer. I tried using the stretch icon but it introduced another issue. There are huge gaps when I resize. I'm not too familiar with layouts and insets etc, so I'm not too sure what is causing this issue. (edited qns to illustrate) – Cherple Oct 18 '22 at 02:27
  • The StretchIcon will either 1) resize to fill the space available or 2) keep the original width/height ratio. In the second case if the width shrinks then the height will also shrink which would cause the gap. – camickr Oct 18 '22 at 04:08

0 Answers0