ok.i have a problem in displaying the images into a jlabel.. the images is stored in mysql database and i used blob as its datatype.. my program is like a search engine.. when the user search for a keyword and click the search button it will show another class which contains the resulted images.. that is my problem..how to show the resulted images when the search button is click..here is my main class

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.BorderFactory.*;
import javax.swing.border.Border.*;
import javax.swing.border.TitledBorder.*;
import java.text.SimpleDateFormat;
//import java.sql.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.io.InputStream;

public class Yeah extends JFrame implements ActionListener
{
    ImageIcon icon = new ImageIcon();
    String filename;
    File file;
    JFileChooser _fileChooser;
    JLabel l1,l2,l3,l4,lph;
    JLayeredPane lp1,lp2,lp3,lp4;
    JPanel np,cp,sp,p,pp;
    JButton searchb,uploadb;
    JTextField txt1;
    InputStream in,in1,in2;
    BufferedImage bimg,bimg1,bimg2;
    
    public Yeah()
    {
        JFrame frame=new JFrame("Yeah");
        setSize(510,450);
        l3 = new JLabel ("Search by keyword: ");
        l4 = new JLabel ("or upload image");
        _fileChooser = new JFileChooser();
        
        lph = new JLabel();
        l1 = new JLabel();
        
        txt1 = new JTextField("Enter Keyword Here",10);
        
        lp1 = new JLayeredPane();
        lp2 = new JLayeredPane();
        lp3 = new JLayeredPane();
        lp4 = new JLayeredPane();
        
        p = new JPanel();
        np = new JPanel();
        cp = new JPanel();
        sp = new JPanel();
        
        uploadb = new JButton("Upload");
        uploadb.addActionListener(this);
        lp3.setPreferredSize(new Dimension(150,20));
        lp3.setLayout(new GridLayout(1,3,4,6));
        lp3.add(uploadb);
        
        searchb = new JButton("Search");
        searchb.addActionListener(this);
        lp3.setPreferredSize(new Dimension(150,20));
        lp3.setLayout(new GridLayout(1,3,4,6));
        lp3.add(searchb);
        
        pp = new JPanel();
        np.setLayout(new GridLayout(1,2));
        lp1.setBorder(BorderFactory.createTitledBorder(""));
        lp1.setPreferredSize(new Dimension(400,450));
        lp1.setLayout(new GridLayout(9,1,4,6));
        lp1.add(l3);
        lp1.add(txt1);
        lp1.add(l4);
        lp1.add(uploadb);
        lp1.add(searchb);
            
        Font g=new Font("serif",1,20);
        np.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"IMAGINE",javax.swing.border.TitledBorder.CENTER,javax.swing.border.TitledBorder.ABOVE_TOP,g,Color.red));
        np.add(lp1);
        
        lp2.setBorder(BorderFactory.createTitledBorder("Uploaded Photos"));
        lp2.setPreferredSize(new Dimension(250,250));
        lp2.setLayout(new GridLayout(1,0));
        lp2.add(lph);
            
        pp.add(lp2);
        pp.add(lp3);
        np.add(pp);
        
        getContentPane().add(np,"North");
        
        
        getContentPane().add(cp,"Center");
        
        addWindowListener(new WindowAdapter()
        {
            public void WindowClosing(WindowEvent we)
            {
                System.exit(0);
            }
        });
        show();
    }
    public void actionPerformed(ActionEvent ae)
    {
        try
        {
            String str1= ae.getActionCommand();
            if(str1.equals("Search"))
            {
                new yeahFrame();
            }
            if(str1.equals("Upload"))
            {
                int retval = _fileChooser.showOpenDialog(Yeah.this);
                    if(retval==JFileChooser.APPROVE_OPTION)
                    {
                        file = _fileChooser.getSelectedFile();
                        filename = file.getPath();
                        icon = new ImageIcon(filename); 
                        lph.setIcon(icon);
                    }
            }
        }
        catch (Exception e)
        {
        }
    }   
        public static void main(String args[])
        {
            new Yeah();
        }
    }

Recommended Answers

All 7 Replies

my problem..how to show the resulted images when the search button is click

You need a couple of things added to your code.
Definition for the yeahFrame class.
Code to create the image.
Code to show the image.

You need a couple of things added to your code.
Definition for the yeahFrame class.
Code to create the image.
Code to show the image.

here is my yeahFrame class

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.io.*;
import java.awt.image.*;


public class yeahFrame extends JFrame
{
    JLabel m1,m2,m3,m4;
    BufferedImage bimg;
    JPanel jp;
    JLayeredPane p1,p2;
    InputStream is;
    
    public yeahFrame()
    {
        JFrame frame = new JFrame("Search Photos");
        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
   
        frame.setSize(1024,750);
        frame.setLocation(20,20);
        frame.setVisible(true);
        
        m1 = new JLabel();
        m2 = new JLabel();
        m3 = new JLabel();
        m4 = new JLabel();
        
        p1 = new JLayeredPane();
        p2 = new JLayeredPane();
        
        jp = new JPanel();
        
        
    }
    public static void main(String args[])
    {
        new yeahFrame();
    }
}

ok here is the problem..i dont know what to use in order to retrive the images search by the user..what method should i use?

i dont know what is the code to create and show the image..

try
        {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","nbuser");
            
            
            Statement st1=conn.createStatement();
            ResultSet rs = st1.executeQuery("select img from pic where caption LIKE 'h%'");
    
                
            while(rs.next())
            {
                Blob blob = rs.getBlob(1);
                in = blob.getBinaryStream();
            }
            rs.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

i used this one to retrieve images from database but how to display all the resulted images in a jlabel? help please

See the API docs for ImageIO.

ok all i have to do is when i click the "search button", it will performed the query statement..and all the resulted images will be displayed in the yeahFrame() class or the second class..how do i do that??

Several steps involved:
Get the bytes for the image
Convert the bytes to an image icon object
Create a label with that icon or set an existing label's icon

ok all i have to do is when i click the "search button", it will performed the query statement..and all the resulted images will be displayed in the yeahFrame() class or the second class

Well, I can see you know how to add components to a Gui, as you've done that. And, I can see that you know how to read a Blob from the DB, since you do that. So, I assumed, that all you needed was help on how to go from the images binary contents to an Image. Well, as I said see the API docs for ImageIO.

..how do i do that??

Not by waiting on us to do it for you which is what this comment makes it look like you're doing.

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.