0

I am getting files from JFileChooser and showing them by reading with BufferedImage and putting in JLabels but there is a problem that my images are not completely shown in JLabels. Here is my code

public class ImagePreview
{
    JPanel PicHolder= new JPanel();
    public ImagePreview()
    {
        JButton GetImages = new JButton("Browse Images");
        GetImages.addMouseListener(new MouseAdapter() 
        {
            public void mouseClicked(MouseEvent evt) 
            {
                 CreatePreviews();
            };
        });
        PicHolder.add(GetImages);

        JFrame MainFrame = new JFrame("Image Preview");
        MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MainFrame.getContentPane().add(PicHolder);
        MainFrame.pack();
        MainFrame.setVisible(true);
    }
    public void CreatePreviews()
    {
        JFileChooser chooser = new JFileChooser();
        chooser.setMultiSelectionEnabled(true);
        File[] selectedCarImages = chooser.getSelectedFiles();
        for(int a=0; a<selectedImages.length; a++)
        {
            try
            {
                BufferedImage myPicture = ImageIO.read(new File(selectedImages[a].getAbsolutePath()));
                JLabel picLabel = new JLabel(new ImageIcon(myPicture));
                PicHolder.add(picLabel);
            }
        }
    }

    public static void main(String[] args) 
    {
          java.awt.EventQueue.invokeLater(() -> {
                 new ImagePreview();
          });
    }
}

When I run this code, it shows user selected images but they are kind of automatically croped and not showing completely in JLabels.

What's wrong here? Why JLabels do not show full images?

  • 1
    `JButton GetImages = new JButton("Browse Images");` Please learn common Java naming conventions (particularly in regard to the case used for class, method and attribute names) and use it consistently. – Andrew Thompson Oct 05 '14 at 14:21
  • There should probably be a `JScrollPane` somewhere in that mix. – Andrew Thompson Oct 05 '14 at 14:25

1 Answers1

3

You're adding all the components and images to a single panel having the default FlowLayout. Instead, use GridLayout for the picture labels and add the browse button to the frame's default BorderLayout, as shown below.

image

As tested:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class ImagePreview {

    JFrame mainFrame = new JFrame("Image Preview");
    JPanel picHolder = new JPanel(new GridLayout(0, 1));

    public ImagePreview() {
        JButton getImages = new JButton("Browse Images");
        getImages.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent evt) {
                CreatePreviews();
            }
        });
        mainFrame.add(getImages, BorderLayout.NORTH);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.add(new JScrollPane(picHolder));
        mainFrame.pack();
        mainFrame.setLocationByPlatform(true);
        mainFrame.setVisible(true);
    }

    public void CreatePreviews() {
        JFileChooser chooser = new JFileChooser();
        chooser.setMultiSelectionEnabled(true);
        chooser.showOpenDialog(mainFrame);
        File[] selectedImages = chooser.getSelectedFiles();
        for (int a = 0; a < selectedImages.length; a++) {
            try {
                BufferedImage myPicture = ImageIO.read(new File(selectedImages[a].getAbsolutePath()));
                JLabel picLabel = new JLabel(new ImageIcon(myPicture));
                picHolder.add(picLabel);
                mainFrame.pack();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(() -> {
            new ImagePreview();
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • But a GridLayout will cause all the labels to have the same size where as flow layout would have been trying to use the preferred size of the images...all be wanting to lay them out in a straight line...A WrapLayout and scroll pane might be better...IMHOTEP – MadProgrammer Oct 05 '14 at 20:30
  • 1
    @MadProgrammer: Worse, `GridLayout` makes them all the size of the _largest_; scaling is a different problem; I agree about `WrapLayout`; `I've used `JList` with a custom render in this context, too. – trashgod Oct 05 '14 at 21:21
  • 1
    Perhaps we need a WrapGridLayout which produces a grid (which the largest cell is the size of the largest preferred size component), but which honours the individual components preferred sizes and allows you to wrap vertically or horizontally...but about here, I'm think JList looks like a better idea :P – MadProgrammer Oct 05 '14 at 21:26
  • Or a grid/list of [thumbnails](http://stackoverflow.com/q/15961412/230513); mine have geometry, but yours have ponies! – trashgod Oct 05 '14 at 22:11