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?

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.