我尝试更改每一行的背景颜色。问题是只有第一个 COLUMN 受到影响。为什么?
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer()
{
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
c.setBackground(row % 2 == 0 ? Color.LIGHT_GRAY : Color.WHITE);
return c;
}
});
最佳答案
使用呈现器方法,您需要为表中的每种数据类型编写自定义呈现器。因此,如果您有字符串、数据、整数、 boolean 值,则需要编写 4 个自定义渲染器。
参见 Table Row Rendering对于一种方法,无论表中有多少数据类型,您都可以编写一次代码。这种方法覆盖了 JTable 的 preparerrenderer(...)
方法。
关于java - 如何更改每一行的背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22301575/