0

I want to add one image for each line in a JScrollPane with a JPanel inside. This is the approach I though of:

for(int i=0;i<arrayWithPaths.size();i++){
BufferedImage newPic;
try {
newPic = ImageIO.read(new File("path");
JLabel picLabel = new JLabel(new ImageIcon(newPic));
panel.add(picLabel);
JLabel space = new JLabel("\n");
panel.add(space);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

However this prints the pictures in just one line.

ocram
  • 1,384
  • 6
  • 20
  • 36

2 Answers2

2

How about a completely different approach -- use a JList that is displayed within the JScrollPane, and add ImageIcons to your JList's model. The JList will know how to handle ImageIcons and will display your images. They're even selectable if need be.

And here's another example:

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

@SuppressWarnings("serial")
public class ImageList extends JPanel {
    private static final String BASE_PATH = "http://file.kelleybluebookimages.com/"
            + "kbb/images/content/editorial/";
    private static final String[] PATHS = {
        "2015-acura-tlx-guide-180.jpg",
        "13A420TFSI_01_hrgb-180.jpg",
        "CT_071713_BMW320i_0439-180.jpg",
        "2013-Cadillac-ATS-137-180.jpg",
        "EJ2V1342-180.jpg",
        "2014LexusIS005-180.jpg",
        "2014-volvo-s60-180.jpg",
        "2015-jeep-renegade-profile-180.jpg"
    };
    private DefaultListModel<Icon> listModel = new DefaultListModel<>();
    private JList<Icon> imageJList = new JList<>(listModel);

    public ImageList() throws IOException {
        for (String path : PATHS) {
            String imgPath = BASE_PATH + path;
            URL url = new URL(imgPath);
            BufferedImage img = ImageIO.read(url);
            listModel.addElement(new ImageIcon(img));
        }

        imageJList.setVisibleRowCount(4);
        JScrollPane scrollPane = new JScrollPane(imageJList);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        add(scrollPane);
    }    

    private static void createAndShowGui() {
        JFrame frame = new JFrame("ImageList");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        try {
            frame.getContentPane().add(new ImageList());
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
        }
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
1

Take a look at the Java Swing Layout Manager tutorial

You need to configure your panel object appropriately, using a BoxLayout for example may give you what you need.

PA001
  • 451
  • 3
  • 12