日期:2014-05-20  浏览次数:20650 次

一个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;
}
}
}