0

I inserted my image with other info in my mysql DB with blob (converting it to byte), and I am able to get the image to show it in a jlable .. But Problem arrives when I try to show it in a Jtable ... I am using the DefaultTableModel, and here is my whole code for this .. can some one give me any idea? I searched around a lot and noting solved my problem :( I want to show the images in the last col...am giving only the code for this part.. ...

and if anyone wants to give the gui a try here is the full code -

private void getTableData(){

    //Connection conn=null;
    //Statement st=null;

    try{
        conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/vehicle","root","");
        st = conn.createStatement();
        String sql="SELECT * FROM user";
        ResultSet rs = st.executeQuery(sql);
        DefaultTableModel model = new DefaultTableModel(new String[]{"Name", "Gender", "Mobile Number", "Email", "Position", "User Name", "Privilege", "Photo"}, 0);
         jTableUsers.setModel(model);
        //  jTableUsers.getColumnModel().getColumn(7).setCellRenderer(jTableUsers.getDefaultRenderer(ImageIcon.class));

       // jTableUsers.getColumnModel().getColumn(7).setCellRenderer(new ImageRenderer());
        if(rs.next()){    
        byte[]imagedata= rs.getBytes("image");
            formate = new ImageIcon(imagedata);   //formate is the variable
            showimageF.setIcon(formate); 
        }
        while(rs.next())
        {
            String col1 = rs.getString("f_name");
            String col2 = rs.getString("gender");
            String col3 = rs.getString("mobile");
            String col4 = rs.getString("email");                
            String col5 = rs.getString("position");
            String col6 = rs.getString("user_name");
            String col7 = rs.getString("user_type");
            //String col18 = col18

           // mod.addRow(new Object[]{xx, rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(10), rs.getString(11), rs.getString(12), rs.getString(13), rs.getString(14), rs.getString(15), rs.getString(16), rs.getString(17), rs.getString(18), rs.getString(19), rs.getString(20), rs.getString(21), rs.getString(22), rs.getString(23), newIconImage, rs.getString(25), rs.getString(26), rs.getString(27)});

            model.addRow(new Object[]{col1, col2, col3, col4,col5,col6,col7,formate,});


        }
        //jTableUsers.setModel(model);
    }catch(Exception ex){
        JOptionPane.showMessageDialog(null, ex.getMessage());
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Soykot
  • 138
  • 1
  • 9
  • You can take a look at this question and answers: http://stackoverflow.com/questions/4941372/how-to-insert-image-into-jtable-cell – Freek de Bruijn Apr 17 '15 at 18:38
  • yeah i saw .. but the problem is i don't get it how to do it for my code :( . am new to java and this is my 1st project ,.. can you be a bit more specific ? or maybe write the code and tell me where to put it ? :( – Soykot Apr 17 '15 at 19:05

2 Answers2

1

But Problem arrives when I try to show it in a Jtable

You need to tell the table that the column contains an Icon, then the table will use the appropriate renderer to render the Icon. You do this by overriding the getColumnClass(...) method of your DefaultTableModel

@Override
public Class getColumnClass(int column)
{

    switch (column)
    {
        case 7: return Icon.class();
        default: return Object.class;
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288
  • You can take a look at this question and answers: [How to Insert Image into JTable Cell](http://stackoverflow.com/questions/4941372/how-to-insert-image-into-jtable-cell)... ;-) – Freek de Bruijn Apr 17 '15 at 18:52
  • yeah i got that suggestion .. but the problem is i don't get it how to do it for my code :( . am new to java and this is my 1st project ,.. can you be a bit more specific ? – Soykot Apr 17 '15 at 19:00
  • Why are you trying to read data from a database on your first project? You should first learn the basics of Java. Overriding a method is something that is done all the time. So why don't you try overriding the DefaultTableModel with the code given above. If you have a problem then post the code. You don't learn anything if you don't try. The linked examples shows how to do this the only difference is one piece of code overrides the getColumnClass() method of a JTable and this answer if for the DefaultTableModel, but the concept is identical. – camickr Apr 17 '15 at 19:18
  • Hi, Thanks for your time .. well after editing here is the part of the code now :.. but still am getting the same result as before .. am sure am doing something wrong :( – Soykot Apr 17 '15 at 20:32
  • Did you add a System.out.pritntln(...) statement to the getColumnClass() method to verify the code is being executed and the column value is 7? – camickr Apr 18 '15 at 13:59
0

Thanks for your time .. well after editing here is the part of the code now : private void getTableData(){

    //Connection conn=null;
    //Statement st=null;

    try{
        conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/vehicle","root","");
        st = conn.createStatement();
        String sql="SELECT * FROM user";
        ResultSet rs = st.executeQuery(sql);
        DefaultTableModel model = new DefaultTableModel(new String[]{"Name", "Gender", "Mobile Number", "Email", "Position", "User Name", "Privilege", "Photo"}, 0);
        JTable table=new JTable(model){
        @Override
            public Class getColumnClass(int column)
            {
               switch (column)
                        {
                            case 8: return Icon.class;
                            default: return Object.class;
                        }
                    }}; 
        jTableUsers.setModel(model);
        //  jTableUsers.getColumnModel().getColumn(7).setCellRenderer(jTableUsers.getDefaultRenderer(ImageIcon.class));

       // jTableUsers.getColumnModel().getColumn(7).setCellRenderer(new ImageRenderer());


        if(rs.next()){    
        byte[]imagedata= rs.getBytes("image");
            formate = new ImageIcon(imagedata);   //formate is the variable
            showimageF.setIcon(formate); 
        }
        while(rs.next())
        {
            String col1 = rs.getString("f_name");
            String col2 = rs.getString("gender");
            String col3 = rs.getString("mobile");
            String col4 = rs.getString("email");                
            String col5 = rs.getString("position");
            String col6 = rs.getString("user_name");
            String col7 = rs.getString("user_type");
            //String col18 = col18


            model.addRow(new Object[]{col1, col2, col3, col4,col5,col6,col7,formate,});


        }
       // jTableUsers.setModel(table);

    }catch(Exception ex){
        JOptionPane.showMessageDialog(null, ex.getMessage());
    }
}
Soykot
  • 138
  • 1
  • 9
  • Still am getting the same result as before .. am doing something wrong but i can't figure it out yet :( – Soykot Apr 17 '15 at 20:36
  • Well I counted columns wrong. Your table only has 8 columns, but since the column value is offset from 0, you should be using 7 in the case statement.. – camickr Apr 17 '15 at 23:49
  • hi, still the same .. maybe am using some code in wrong place here ?.. all I get is "javax.swing.ImageIcon@55a45f" -- this kind of codes in image column... is my this declaration okay--" jTableUsers.setModel(model);"?? – Soykot Apr 18 '15 at 12:02
  • So this tells you the data in the TableModel is an ImageIcon, which is good. So the problem must be that your code in the getColumnClass() method is NOT being executed. So this probably means that you have two instances of JTable and you are overriding the wrong instance. Based on the code provided you create the JTable, but you don't add the table to the frame. – camickr Apr 18 '15 at 13:58
  • well that's the problem!! shall i share the whole code so that you might find out what am i doing wrong ? – Soykot Apr 18 '15 at 16:39