我想根据jtable中某一列值改变颜色,可是我只能做到改变该列颜色,怎么改变整行颜色,比如第一列id(int)值>0一种颜色,<=0又一种颜色,代码如下:
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(table, value,isSelected, hasFocus, row, column);
if(column==0){
int intValue = Integer.parseInt(value);
if(intValue>0){
setBackground(Color.gray);
}
else{
setBackground(Color.green);
}
}
return cell;
}
请各位帮忙
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(table, value,isSelected, hasFocus, row, column);
if(column==0){
int intValue = Integer.parseInt(value);
if(intValue>0){
setBackground(Color.gray);
}
else{
setBackground(Color.green);
}
}
return cell;
}
请各位帮忙
if (id > 0) {
setBackground(Color.gray);
}
else {
setBackground(Color.green);
}}
是根据条件改变某一行的
不知道对楼主有没有帮助JTable中,第三列为Y显示一种颜色,第四列为Y显示另一种颜色public static boolean compare(int a, String b) {
int i;
String d = "";
int[] c;
for (i = 0; i < b.length()-1; i++) {
d = b.substring(i, i + 2);
i++;
if (a == Integer.parseInt(d)) return true; } return false; } public static void makeFace(JTable table) { try {
DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
String red = "";
String yellow = "";
int i; for (i = 0; i < table.getRowCount(); i++) {
if (table.getValueAt(i, 2).equals("Y")) {
if (i > 9) {
red = red + String.valueOf(i);
}
else {
red = red + String.valueOf(0) + String.valueOf(i);
}
}
} for (i = 0; i < table.getRowCount(); i++) {
if (table.getValueAt(i, 3).equals("Y")) {
if (i > 9) {
yellow = yellow + String.valueOf(i);
}
else {
yellow = yellow + String.valueOf(0) + String.valueOf(i);
}
}
} if (compare(row, red))
setBackground(Color.red);
else if (compare(row, yellow))
setBackground(Color.yellow);
else
setBackground(Color.white);
return super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column); } };
for (int i = 0; i < table.getColumnCount(); i++) {
table.getColumn(table.getColumnName(i)).setCellRenderer(tcr);
}
}
catch (Exception ex) {
ex.printStackTrace();
} }
http://www.finereport.com