2011-02-23 21 views
2

我想實現一個JTable組件的tablecellrenderer,它應該根據單元格數據顯示不同的顏色。我得到了這個,但我不能改變所選單元格的顏色。我試着這樣做: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

我想實現一個JTable組件的的TableCellRenderer,它應該表現出依賴於細胞的數據

您發佈並沒有這樣做的代碼不同的顏色。基本上所有代碼都會重複渲染器的默認行爲

您可能會發現Table Row Rendering方法更容易實現。

0

假設您使用JLabel作爲組件的基礎,除非您還將opaque設置爲true,否則設置背景將不起作用。 JLabels默認爲不透明,所以不要繪製背景。