首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

一个jtable外观的有关问题

2012-01-21 
一个jtable外观的问题使用jtable创建了一个表格,并把表格中某些行设置了底色(用以区分特殊的数据),现在的

一个jtable外观的问题
使用jtable创建了一个表格,并把表格中某些行设置了底色(用以区分特殊的数据),现在的问题是,当选择一个有底色的行时,该行底色变成了灰色,原来的底色看不见了,请问有什么解决方案。号小分不多,请见谅。

[解决办法]
改写JTable的CellRenderer
public class TableViewRenderer extends DefaultTableCellRenderer {

private Color unselectedForeground;
private Color unselectedBackground;

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {

if (isSelected) {
component.setForeground( table.getSelectionForeground());
component.setBackground(table.getSelectionBackground());
} else {
component.setForeground((unselectedForeground != null) ? unselectedForeground
: table.getForeground());
component.setBackground((unselectedBackground != null) ? unselectedBackground
: table.getBackground());
}
setFont(table.getFont());
setValue(value);
return this;
}
}
}

热点排行