2

我想实现一个 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 没有显示任何不同。

4

2 回答 2

2

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

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

您可能会发现表格行渲染方法更易于实现。

于 2011-02-24T02:16:19.853 回答
0

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

于 2011-03-02T19:03:34.320 回答