1,105,578 Community Members

displaying images stored in mySql database as a grid

Member Avatar
nidheeshkumar.r
Newbie Poster
15 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

when i tried to add more images it fails..

Please explain what fails.

Member Avatar
nidheeshkumar.r
Newbie Poster
15 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
DavidKroukamp
Master Poster
737 posts since Dec 2011
Reputation Points: 105 [?]
Q&As Helped to Solve: 180 [?]
Skill Endorsements: 4 [?]
Team Colleague
Featured
 
1
 

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

Member Avatar
JamesCherrill
... trying to help
10,385 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
1
 

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.

Member Avatar
nidheeshkumar.r
Newbie Poster
15 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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);             
            } 
    }
    }
Question Answered as of 2 Years Ago by NormR1, DavidKroukamp and JamesCherrill
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: