问题
How to set the Image in jTable cell when click(Mouse event) the row?If I select first row the image will display in that row.Then i click the second row, the image will show in the second row?how to do this using table cell renderer or prepare renderer?
回答1:
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
.
回答2:
This is your 4th question on displaying an image in a JTable, so I'm guessing you already know how to do that.
So if you want to update a row when the selection changes then you will need to use a ListSelectionListener. Then when the listener fires you will need to update the TableModel to remove the icon from the previous row and update the icon in the current row.
JList: previous selected item shows you you can get the row numbers to update.
回答3:
The best way to do this, is to make you own table cell renderer.
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
}
Something like this.
来源:https://stackoverflow.com/questions/9225657/how-set-image-in-jtable-cell-when-clickmouse-event-the-row