I am quiet new to Swing. I have a JTable in which images are displayed in each cell. I need to create a RED border only around the cell which is currently selected. To do this I used following renderer class:
public class ImageRenderer extends DefaultTableCellRenderer {
JLabel lbl=new JLabel();
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column )
{
lbl.setIcon((ImageIcon)value);
if(isSelected && hasFocus)
{
lbl.setBorder(BorderFactory.createEtchedBorder(Color.RED, Color.yellow));
}
return lbl;
}
}
The problem I am facing is that when I click on any cell in the JTable then instead of that particular cell the border is displayed for all the cells of the given column. I only need the border around the selected cell and not around all the cells present in that particular column.
Did you try to unset the border if the cell is not selected?
if(isSelected && hasFocus)
{
lbl.setBorder(BorderFactory.createEtchedBorder(Color.RED, Color.yellow));
}else{
lbl.setBorder( BorderFactory.createEmptyBorder() );
}