单击(鼠标事件)该行时如何在 jTable 单元格中设置图像?如果我选择第一行图像将显示在该行中。然后我单击第二行,图像将显示在第二行中?如何做到这一点使用表格单元格渲染器或准备渲染器?
问问题
1529 次
3 回答
2
If you just want the image to appear in the table cell, use the default renderer for ImageIcon
and ensure that your TableModel
returns ImageIcon.class
for that column.
If you want the image to appear in response to a click, consider using a variation of TablePopupEditor
with setClickCountToStart(1)
and your image as an Icon
.
于 2012-02-10T11:46:25.740 回答
1
这是关于在 JTable 中显示图像的第四个问题,所以我猜你已经知道如何做到这一点。
因此,如果您想在选择更改时更新一行,那么您将需要使用 ListSelectionListener。然后,当侦听器触发时,您将需要更新 TableModel 以从前一行中删除图标并更新当前行中的图标。
JList:先前选择的项目显示您可以获得要更新的行号。
于 2012-02-10T16:26:11.267 回答
-2
最好的方法是让您拥有自己的表格单元格渲染器。
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if(isSelected){
return new Image(); // if selected
}
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); // if not selected do the normal stuff
}
像这样的东西。
于 2012-02-10T10:32:23.667 回答