我在程序中有表格,我使用类设计Zebra颜色表:
private class ColorTableCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
final Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
table.setIntercellSpacing(new Dimension(0,0));
if(!isSelected) {
if (row%2 == 0) {
cellComponent.setBackground(new Color(255,255,254));
} else {
cellComponent.setBackground(new Color(170,170,170));
}
}
return cellComponent;
}
}
当通过:调用该类时
mytable.setDefaultRenderer(Object.class, new ColorTableCellRenderer());
需要非常大的CPU资源(大约50%)。 当使用DefaultTableCellRenderer时,请调用:
mytable.setDefaultRenderer(Object.class, new DefaultTableCellRenderer());
仅需1%的CPU资源。 我尝试使用此:
private class ColorTableCellRenderer extends DefaultTableCellRenderer {
JTable table = new JTable()
{
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
{
Component c = super.prepareRenderer(renderer, row, column);
table.setIntercellSpacing(new Dimension(0,0));
System.out.println("Coba ");
// Alternate row color
if(row%2 == 0) {
c.setBackground(Color.YELLOW);
}
else {
c.setBackground(Color.GREEN);
}
return c;
}
};
}
未发生任何事情(失败)。 也许有人可以修复上面的ColorTableCellRenderer类以减少使用cpu资源。谢谢。