OK, hello guys.

I'm trying to make an image fit a JPanel. What I mean is to set a maximum size on a image, depending on how big the JPanel is.

I'm using a JSplitPane to show some images. On the lef side I'm using a JList, and on the right I'm using a GridLayout(2,1) with a JPanel and JTextArea. For each picture I select, I want a description. The thing is, I managed to show the picture in the JPanel with text etc. B

But if I put a small pic, it's small. If I put a big picture, I can't see the whole picture. I want to set a size on my pictures so it fills the JPanel without missing any content. I don't care if the pictures get bad quality (for example a small picture would get bad quality if it's resized to fit a big JPanel).

But I'm not quite sure how I can do this.

At the moment, this is how I upload the image.

if(result == JFileChooser.APPROVE_OPTION) {
            thePanel.removeAll();
            thePanel.repaint();
            File image = chooser.getSelectedFile();
            ImageIcon photo = new ImageIcon(image.getAbsolutePath());
            JLabel label = new JLabel(image.getName(), photo, JLabel.RIGHT);
            thePanel.add(label, BorderLayout.CENTER);
            thePanel.repaint();
            thePanel.revalidate();
            return;
}   

How can I make my picture big enough to fit the JPanel?

Thanks in advance.

The Image class has a getScaledInstance method that scales the Image to whatever size you want (eg the size of your JPanel)

That code i showed above was an example. In my real application, I'm using a GridLayout to show a JPanel and JTextArea. So it's not a fixed size on the JPanel, since the frame can be maximized or minimized.
So I want an image to fit that panel..

You may need to add a ComponentListener to the JPanel and in its componentResized method re-scale the image to the new size.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.