Jason S
Jason S

Reputation: 189656

using a JLabel+icon with JTable cellrenderer + selection highlight

I have a JTable with one column that has a custom cell renderer that shows one of several icons.

It works well except the selection highlight does not automatically appear, and I don't know how to apply a highlight in my custom cell renderer.

Any suggestions?

Upvotes: 1

Views: 1228

Answers (3)

camickr
camickr

Reputation: 324118

I have a JTable with one column that has a custom cell renderer that shows one of several icons.

JTable supports the display of Icons. Just add your Icon to the model and then override the getColumnClass(...) method to return Icon and the proper renderer will be used.

Upvotes: 2

jzd
jzd

Reputation: 23629

In your getTableCellRendererComponent() method there is a parameter (boolean isSelected), that indicates when the row is selected. You will need to check that and do highlighting yourself in the renderer.

Upvotes: 0

maneesh
maneesh

Reputation: 1701

In your renderer code, you will have to explicitly set the background in case of selection. The usual way to do it is asking the UIManager to provide you the color for Table.background and Table.selectionBackground

Upvotes: 1

Related Questions