Soykot
Soykot

Reputation: 138

Showing Image From Mysql (blob) to Jtable- DefaultTableModel

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());
    }
}

Upvotes: 0

Views: 2521

Answers (2)

camickr
camickr

Reputation: 324128

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;
    }
}

Upvotes: 1

Soykot
Soykot

Reputation: 138

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());
    }
}

Upvotes: 0

Related Questions