Question

Ok, so I've no problem getting a single image from a database. I use this to return it as an ImageIcon on a JLabel;

while (rs.next()) {
images = rs.getString("path");
System.out.println(images + "\n");
System.out.println("TESTING - READING IMAGE");
BufferedImage img = ImageIO.read(new File(images));
System.out.println("img = " + images);
imagelabel = new JLabel(new ImageIcon(img));
imagelabel.setPreferredSize(new Dimension(200, 200));
imageselect.add(imagelabel);

However, I need to do this with multiple images, and assign each JLabel to a new JPanel in a CardLayout. I know I need some sort of Loop, looking for suggestions on the best way to do this!

BufferedImage imgA = ImageIO.read(new File("lmkpackage/images/one.jpg"));
            image1 = new JLabel(new ImageIcon(imgA));
            image1.setPreferredSize(new Dimension(200, 200));
            img1 = new JPanel();        
            img1.add(image1);

            loadcard.add(img1,"1");
            cl2.show(loadcard,"1");

            BufferedImage imgB = ImageIO.read(new File("lmkpackage/images/two.jpg"));
            image2 = new JLabel(new ImageIcon(imgB));
            image2.setPreferredSize(new Dimension(200, 200));
            img2 = new JPanel();        
            img2.add(image2);

            loadcard.add(img2, "2");

            BufferedImage imgC = ImageIO.read(new File("lmkpackage/images/three.jpg"));
            image3 = new JLabel(new ImageIcon(imgC));
            image3.setPreferredSize(new Dimension(200, 200));
            img3 = new JPanel();        
            img3.add(image3);

            loadcard.add(img3, "3");

            BufferedImage imgD = ImageIO.read(new File("lmkpackage/images/four.jpg"));
            image4 = new JLabel(new ImageIcon(imgD));
            image4.setPreferredSize(new Dimension(200, 200));
            img4 = new JPanel();
            img4.add(image4);

            loadcard.add(img4, "4");

            BufferedImage imgE = ImageIO.read(new File("lmkpackage/images/five.jpg"));

            image5 = new JLabel(new ImageIcon(imgE));
            image5.setPreferredSize(new Dimension(200, 200));
            img5 = new JPanel();        
            img5.add(image5);

            loadcard.add(img5, "5");

Here is my attempt at it, as requested:

while (rs.next()) {
                        images = rs.getString("path");
                        System.out.println(images + "\n");
                        System.out.println("TESTING - READING IMAGE");
                        for(i=0; i < 5; i++){                       
                        BufferedImage img[i] = ImageIO.read(new File(images));
                        imglab[i] = new JLabel(new ImageIcon(imgIcon[i]));
                        imgPanel[i]= new JPanel();
                        imgPanel[i].add(imglab[i]);
                        loadcard.add(imgPanel[i], i);                                       
                        }//End For
                    }//EndWhile

And the errors Im getting are:

letmeknow.java:181: ']' expected BufferedImage img[i] = ImageIO.read(new File(images)); letmeknow.java:181: illegal start of expression BufferedImage img[i] = ImageIO.read(new File(images));

Was it helpful?

Solution

This line makes no sense:

BufferedImage img[i] = ImageIO.read(new File(images));

as you appear to be trying to declare and use an array at the same time, and this suggests that you should review a basic Java tutorial on the use of arrays since this knowledge base is critical and should be well known before trying either database programming or Swing GUI programming.

To possibly solve this, declare your array (or possibly better -- ArrayList) of BufferedImage before the while loop, and then use it inside the loop. For example:

// !!! CAVEAT: code not compiled nor tested !!!

// TOTAL_IMAGE_COUNT is a constant that defines the array size
// an ArrayList might be better though
BufferedImage[] myImages = new BufferedImage[TOTAL_IMAGE_COUNT];
int i = 0;
while (rs.next()) {
    String imagePath = rs.getString("path");
    System.out.println(imagePath + "\n");
    System.out.println("TESTING - READING IMAGE");

    myImages[i] = ImageIO.read(new File(imagePath));
    imglab[i] = new JLabel(new ImageIcon(myImages[i]));
    imgPanel[i]= new JPanel();
    imgPanel[i].add(imglab[i]);
    loadcard.add(imgPanel[i], i);     
    i++;                 
}//EndWhile

Although all of these arrays might not even be needed if all you're doing is adding JPanels into a CardLayout. And it seems a bit strange to me that you're storing the image file path in a database rather than the image itself. Your image file names seem trivial enough that perhaps the database isn't even needed. Perhaps all you need is something very simple like this:

  String imageLocation = "lmkpackage/images/";
  String[] imageNames = {"one", "two", "three", "four", "five"};
  String imgExt = ".jpg";

  int count = 1;

  for (String imageName : imageNames) {
     String imagePath = imageLocation + imageName + imgExt;
     BufferedImage img = ImageIO.read(new File(imagePath));
     ImageIcon icon = new ImageIcon(img);
     JLabel label = new JLabel(icon);
     loadcard.add(label, String.valueOf(count));
     count++;
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top