hey,

i have stored a few images in mySQL database as BLOB.Now i want to retrieve and display the images on frame as a gallery/grid view.I have displayed one image in a frame but when i tried to add more images it fails...Can anyone help with sample code,coz im new to the language....

I've tried the following code,bt it didnt worked:-

public class ImageShow extends JFrame{
  Image image;
  public int x = 30,y=30; 
  ResultSet r;
  public ImageShow(){
  setTitle("Image Retrieved");
  setSize(1000,1000);
  addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent we){
  setVisible(false);
  }
  });
  setVisible(true);
  }
  
  public void paint(Graphics g){
        try {
            r = ImageRetrieve.rs;           
            while (r.next())
                {                 
                    byte[] imagedata = r.getBytes("image_path") ;
                    image = Toolkit.getDefaultToolkit().createImage(imagedata);
                    //Toolkit tool = Toolkit.getDefaultToolkit();
                    g.drawImage(image,x,y,this);
                    x+=30;
                    y+=30;
                 } 
            } 
            catch (SQLException ex) {
                                    Logger.getLogger(ImageShow.class.getName()).log(Level.SEVERE, null, ex);
                                    }  
    }
}

Is there any better way?

Dani AI

Generated

Brief summary and practical improvements based on the thread: fixed the original problem by switching from custom painting to a GridLayout of JLabels (consistent with 's advice to avoid heavy IO in paint). The following notes tighten that solution for reliability, responsiveness, and image quality.

Prefer decoding BLOB bytes with ImageIO.read(new ByteArrayInputStream(bytes)) to obtain a fully loaded BufferedImage (Toolkit.createImage is asynchronous and often requires MediaTracker/ImageObserver). For good-looking thumbnails, scale via Graphics2D with rendering hints rather than relying on getScaledInstance.

private BufferedImage scale(BufferedImage src, int w, int h) {
  BufferedImage dst = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  Graphics2D g2 = dst.createGraphics();
  g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
  g2.drawImage(src, 0, 0, w, h, null);
  g2.dispose();
  return dst;
}

Load from the database off the Event Dispatch Thread and publish results back to the UI to keep the GUI responsive. A SwingWorker that reads rows, builds ImageIcons (from scaled BufferedImages) and calls publish/process to add JLabels to the panel is a solid pattern. Add all components to the panel, call panel.revalidate()/repaint() as chunks arrive, and avoid calling pack() or setVisible(true) inside row-processing loops.

Additional tips: put the image panel inside a JScrollPane for large galleries; close ResultSet/Statement/Connection in finally blocks; generate and store thumbnails if many images are served; for large-scale apps prefer storing files in the filesystem or object storage and keep only paths/metadata in the database to reduce DB load and memory pressure.

Recommended Answers

All 7 Replies

when i tried to add more images it fails..

Please explain what fails.

Please explain what fails.

On execution i only gets a new window,its not displaying any images....i heard about grid layout,can i use that here?? wil u help me with some examples?

On execution i only gets a new window,its not displaying any images....i heard about grid layout,can i use that here?? wil u help me with some examples?

maybe see here:http://stackoverflow.com/questions/8500746/java-swing-displaying-multiple-images-dynamically-on-jpanel and here:http://answers.yahoo.com/question/index?qid=20100704125651AAN0AUm
[edit]and this too is helpful:http://docs.oracle.com/javase/7/docs/api/java/awt/GridLayout.html

Rather than overriding paint, you;ll be on easier ground by creating a gridlayout of JLabels and using ImageIcon to place your graphics in the JLabels. (just Google it)
If you must override paint then don't re-load the images from disk every time paint is called. It can get called very frequently and all that repeated IO will grind your GUI to snails-pace. Load them all into memory once when the program starts.

hey,
thanks to everyone who commented and helped me...
i've solved my problem...i used gridlayout for displaying the images and it worked...
code :-

public class ImageShow extends JFrame{
    
    ResultSet r;
    Image img;
   public ImageShow() throws SQLException
    {
        setTitle("Image retrieved");
        setSize(500, 500);
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        Container pane = getContentPane();
        pane.setLayout(new GridLayout(3,3));
        r=ImageRetrieve.rs;
        while (r.next())
            {
                byte[] imagedata = r.getBytes("image_path") ;
                img = Toolkit.getDefaultToolkit().createImage(imagedata);
                img = img.getScaledInstance(200,200,Image.SCALE_SMOOTH);
                ImageIcon icon =new ImageIcon(img);
                JLabel Photo = new JLabel(icon) ;                                  
                pane.add(Photo) ;                
                this.pack();
                this.setVisible(true);             
            } 
    }
    }

Hey What is ImageRetrieve

It looks like that's another of nidheeshkumar.r's classes that goes alongside ImageShow and handles getting the image ResultSet from the database.

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.