如何将渲染器仅应用于JTable中的特定单元而不是整列?

时间:2012-02-20 21:17:49

标签: java swing jtable renderer border

我很安静新手。我有一个JTable,其中每个单元格中都显示图像。我需要在当前选中的单元格周围创建一个RED边框。为此,我使用了以下渲染器类:

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;


 }
} 

我面临的问题是当我点击JTable中的任何单元格而不是该特定单元格时,将显示给定列的所有单元格的边框。我只需要选定单元格周围的边框,而不是该特定列中存在的所有单元格。

请帮助我解决这个问题(非常紧急。)

提前致谢

1 个答案:

答案 0 :(得分:7)

如果未选择单元格,您是否尝试取消设置边框?

     if(isSelected && hasFocus)
     {
         lbl.setBorder(BorderFactory.createEtchedBorder(Color.RED, Color.yellow));
     }else{
         lbl.setBorder( BorderFactory.createEmptyBorder() );
     }