简体   繁体   English

显示图像从Mysql(blob)到Jtable-DefaultTableModel

[英]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? 我将图像和其他信息插入到具有blob的mysql数据库中(将其转换为字节),并且能够将图像显示为jlable ..但是,当我尝试在Jtable中显示它时,问题就来了。我正在使用DefaultTableModel,这是我的整个代码..有人可以给我任何想法吗? 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 - 如果有人想给gui尝试一下, 这里是完整的代码-

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

But Problem arrives when I try to show it in a Jtable 但是,当我尝试在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. 您需要告诉表该列包含一个Icon,然后该表将使用适当的渲染器来渲染Icon。 You do this by overriding the getColumnClass(...) method of your DefaultTableModel 您可以通过重写DefaultTableModelgetColumnClass(...)方法来执行此操作

@Override
public Class getColumnClass(int column)
{

    switch (column)
    {
        case 7: return Icon.class();
        default: return Object.class;
    }
}

Thanks for your time .. well after editing here is the part of the code now : private void getTableData(){ 多谢您的宝贵时间。.编辑完后,这是代码的一部分: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());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM