TableCellRenderer选择了单元格问题

时间:2011-02-23 22:31:16

标签: java swing jtable tablecellrenderer

我想实现一个JTable组件的tablecellrenderer,它应该根据单元格数据显示不同的颜色。我得到了这个,但我不能改变所选单元格的颜色。我试着这样做:

public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex)
{

    if (isSelected) {
        this.setBackground((Color)UIManager.get("Table.selectionBackground"));
        this.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
    } else {
        this.setForeground((Color)UIManager.get("Table.foreground"));  
        this.setBackground((Color)UIManager.get("Table.background"));  
        this.setBorder(BorderFactory.createEmptyBorder()); 
    } 
...
}

但它不起作用:S ..我看不到问题,因为当我点击一个单元格时,JTable没有显示任何不同。

2 个答案:

答案 0 :(得分:2)

  

我想实现一个JTable组件的tablecellrenderer,它应该根据单元格数据显示不同的颜色

您发布的代码不会这样做。基本上所有代码都会复制渲染器的默认行为

您可能会发现Table Row Rendering方法更易于实施。

答案 1 :(得分:0)

假设您使用JLabel作为组件的基础,除非您还将opaque设置为true,否则设置背景将无效。 JLabels默认不透明,因此不要绘制背景。

相关问题