[英]Change row Background in JTable Java
我的項目中有一個名為tasktable
的表。 它從數據庫(oracle)中檢索數據。 如何更改單元格ex中具有顏色的行的顏色。 (i,8)當我點擊刷新按鈕時會自動?
我已經嘗試過很多次了,以將源代碼放在特定的行上,但是最終使所有表着色:
int count;
count = tasktable.getRowCount();
for (int i=0;i<count;i++)
{
rr = new Object ();
rr = tasktable.getModel().getValueAt(i,8);
if(rr.equals("GREEN"))
{
setBackground(Color.GREEN);
}
if(rr.equals("red"))
{
setBackground(Color.red);
}
if(rr.equals("BLUE"))
{
setBackground(Color.BLUE);
}
if(rr.equals("yellow"))
{
setBackground(Color.yellow);
}
if(rr.equals("pink"))
{
setBackground(Color.pink);
}
if(rr.equals(null))
{
setBackground(null);
}
如何解決這個問題?
將自定義TableCellRenderer添加到您的表。
setBackground()
設置JTable
的背景色,而不是每行或單元格的背景色。 您需要一個TableCellRenderer
,如@Recursed所述。
如果您要做的只是更改行顏色,請為JTable
子類化並重寫prepareRenderer
方法:
public Component prepareRenderer(TableCellRenderer renderer,
int row,
int column) {
Component c = super.prepareRenderer(renderer, row, column);
if (row == HIGHLIGHT_ROW) {
c.setBackground(BG_COLOR);
}
return c;
}
Component comp = super.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, col);
String s = table.getModel().getValueAt(row, VALIDATION_COLUMN ).toString();
comp.setForeground(Color.red);
http://www.java-forums.org/awt-swing/541-how-change-color-jtable-row-having-particular-value.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.