How to set image in jTable cell when clicked (mouse event) in row?

How to set image in jTable cell when clicking (mouse event) row? If I select the first line, the image will be displayed on that line. Then, when I press the second line, will the image be displayed on the second line? do this using the table cell renderer or prepare a render?

0
source share
3 answers

If you want the image to appear in a table cell, use the default rendering for ImageIcon and make sure your TableModel returns ImageIcon.class for this column.

If you want the image to appear in response to a click, consider the option TablePopupEditor with setClickCountToStart(1) and your image as an Icon .

+2
source

This is your fourth question about displaying an image in JTable, so I assume you already know how to do this.

So, if you want to update the row when the selection changes, you will need to use a ListSelectionListener. Then, when the listener starts, you will need to update the TableModel to remove the icon from the previous line and update the icon in the current line.

JList: The previous item selected shows that you can get line numbers for the update.

+1
source

The best way to do this is to create your own rendering of table cells.

 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 that.

-2
source

All Articles