设置swing table中特定属性的行颜色

cbly1996 2007-11-21 01:50:52
如果有一个表:

Name    Age    Sex

Bob    23    M
Claire   99    F
Spot    7   F
Phil   69   M

如果我要使Sex属性值为M的行的颜色为蓝色,我知道要用到getTableCellRendererComponent
大家请看:
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 ( value=="M"){

        ?????

}
return cell;
}
请大家帮我看看?????区域里的代码该怎么样写啊
...全文
248 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
cbly1996 2007-11-22
  • 打赏
  • 举报
回复
我调试了下,table.getValueAt(row,sexCloumn)的值确实是M,可为什么所有的行都变成蓝色了呢
AppleYang2006 2007-11-21
  • 打赏
  • 举报
回复
不会啊,肯定可以的
int sexCloumn = table.convertColumnIndexToView(2)
if ("M".equals(table.getValueAt(row,sexCloumn)) {
cell.setBackground(Color.BLUE);
}
你调试下,看看table.getValueAt(row,sexCloumn)的值,如果是"M"肯定是没问题的
cbly1996 2007-11-21
  • 打赏
  • 举报
回复
我刚才试了一下,还是不行啊~~
AppleYang2006 2007-11-21
  • 打赏
  • 举报
回复
判断条件改下
int sexCloumn = table.convertColumnIndexToView(性别的列索引)
if (table.getValueAt(row,sexCloumn) == "M") {
cell.setBackground(Color.BLUE);
}
我暂时想到的办法,你试试
cbly1996 2007-11-21
  • 打赏
  • 举报
回复
似乎很有难度,我可以加分,急啊~~~
cbly1996 2007-11-21
  • 打赏
  • 举报
回复
这么写只能使有M值的那个单元格变为蓝色,而我现在想使有M值的整个那一行都为蓝色!!是整个行而不是一个单元格
AppleYang2006 2007-11-21
  • 打赏
  • 举报
回复
cell.setBackground(Color.BLUE);

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧