如何从JTable中获取图标
我已使用以下代码更改了 JTable
中的单元格渲染以显示图像而不是文本:
base_table.getColumnModel().getColumn(3).setCellRenderer(new TableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable jtable, Object value,
boolean bln, boolean bln1, int i, int i1) {
JLabel lbl = new JLabel();
lbl.setIcon((ImageIcon) value);
return lbl;
}
});
现在,我希望能够获取 JTable
中每一行的图像code> 以便将其保存在数据库中。我怎么能这么做呢?
I have changed the cell render in JTable
to show image instead of text using the following code:
base_table.getColumnModel().getColumn(3).setCellRenderer(new TableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable jtable, Object value,
boolean bln, boolean bln1, int i, int i1) {
JLabel lbl = new JLabel();
lbl.setIcon((ImageIcon) value);
return lbl;
}
});
Now, I'd like to be able to get the image for each row in the JTable
in order to save it in database. How could I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我无法抗拒这个
I can't resist just example for that
存储在
JTable
中的数据可以在其TableModel
中找到。但由于通常是您的代码构建此TableModel
(通常从列表或数组),因此您应该能够从此列表或数组中获取图标。否则,只需使用table.getModel().getValueAt(row, column)
,并将其转换为ImageIcon
。The data stored in a
JTable
can be found in itsTableModel
. But since it's your code, normally, that builds thisTableModel
(from a list or an array, typically), you should be able to get the icon from this list or array. Else, just usetable.getModel().getValueAt(row, column)
, and cast it to anImageIcon
.您的表格模型中应该已经包含了所有图像。因此,您只需从模型中获取图像,然后将它们保存在数据库中。
在单元格渲染器中,您具有类型
Object value
,然后使用(ImageIcon) value
将其转换为lbl 中的
ImageIcon
.setIcon((ImageIcon) value);当您从模型中获取数据时,您可以执行相同的操作:
其中 3 是包含图像的列的
columnIndex
,rowIndex 是排你 想。You should already have all the images in your table model. So you just have to get the images from the model, and then save them in your database.
In your cell renderer you have the type
Object value
, then you use(ImageIcon) value
to cast it to anImageIcon
inlbl.setIcon((ImageIcon) value);
You can do the exaclty the same when you get the data from your model:
where 3 is your
columnIndex
for the column with images, and rowIndex is the row you want.