Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7527387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:05:38+00:00 2026-05-30T04:05:38+00:00

Ok, so I’ve no problem getting a single image from a database. I use

  • 0

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));

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:05:39+00:00Added an answer on May 30, 2026 at 4:05 am

    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++;
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.