简体   繁体   English

将数据库中的图像添加到JTable中

[英]add an image from Database into JTable

i am trying to add an image that is in my database in a column in JTable with a number and a code special to this image the problem is i got the number and the code but the image doesn't show i don't know why here is my code 我正在尝试在数据库中的JTable列中添加一个图像,该图像带有数字和该图像的特殊代码,问题是我得到了数字和代码,但是图像未显示我不知道为什么这是我的代码

String sql="SELECT id,chiffre,image FROM symbolique WHERE id  BETWEEN 200 and 205";
try{
    st=connexion.prepareStatement(sql);
    res=st.executeQuery();
    while(res.next()){
        int j=0;
        String [] code= new String[1000];
        String [] chiffre1=new String[100];
        code [j] = res.getString("id");
        chiffre1[j] = Integer.toString(res.getInt("chiffre"));
        Blob blob = res.getBlob("image");
        is2 = new BufferedInputStream(blob.getBinaryStream());
        Image raw = ImageIO.read(is2);
        ImageIcon icon  =new ImageIcon(raw);
        Object [][] data = new Object[200][100];
        data[j][1]= code [j];
        data[j][2]= chiffre1[j];
        data[j][3]=icon;
        j++;
        String title[] = {"image", "chiffre","code"};
        DefaultTableModel model = new DefaultTableModel(data, title);
        table.setModel(model);
    }
}catch(Exception e){
   JOptionPane.showMessageDialog(null, e);}
}
  • put Icon/ImageIcon to JTable better to the TableModel directly 直接将Icon/ImageIcon放到JTableTableModel更好

  • you have to override getColumnClass for Icon/ImageIcon in XxxTableModel for showing this Object as image in the JTables view 你必须覆盖getColumnClassIcon/ImageIconXxxTableModel用于显示该Object为图像JTables视图

  • for detailed informations (incl. working code examples) to read official Oracle tutorial - How to use Tables 有关详细信息(包括工作代码示例)以阅读Oracle官方教程- How to use Tables

This won't help solve the image problem but right now your code recreates the TableModel for every row in the ResultSet, which means you will only ever see one row of data. 这将无助于解决图像问题,但是现在您的代码为ResultSet中的每一行重新创建TableModel,这意味着您只会看到一行数据。

  1. Since you have looping code you need to create an empty DefaultTableModel before the loop starts. 由于您具有循环代码,因此需要在循环开始之前创建一个空的DefaultTableModel。
  2. Inside the loop you then use the addRow(...) method to add another row of data to the TableModel for every row in the ResultSet 然后,在循环内部,使用addRow(...)方法为ResultSet中的每一行向TableModel添加另一行数据
  3. When you finish the loop then you use the model to create the JTable. 完成循环后,可以使用模型创建JTable。

Did you add any debug code? 您是否添加了任何调试代码? Did you attempt to display the width/height of the image to confirm that you are reading the image properly? 您是否尝试显示图像的宽度/高度以确认您正在正确读取图像? Why don't you do a simple test that tries to read and display an image in a JLabel first since using a JTable is more complicated than using a JLabel. 为什么不做一个简单的测试,尝试首先在JLabel中读取和显示图像,因为使用JTable比使用JLabel更复杂。 Then once you get that working you can apply the knowledge to using a JTable. 然后,一旦您开始工作,就可以将知识应用于使用JTable。

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

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