[英]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
放到JTable
到TableModel
更好
you have to override getColumnClass
for Icon/ImageIcon
in XxxTableModel
for showing this Object
as image in the JTables
view 你必须覆盖
getColumnClass
的Icon/ImageIcon
在XxxTableModel
用于显示该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,这意味着您只会看到一行数据。
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.