[英]How to change the background color of each second row?
I try to change the background color of each second row. 我尝试更改第二行的背景色。 The problem is that only first COLUMN is affected. 问题在于仅第一个COLUMN会受到影响。 Why? 为什么?
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;
}
});
Using the renderer approach you need to write a custom renderer for every data type in the table. 使用渲染器方法,您需要为表中的每种数据类型编写一个自定义渲染器。 So if you have String, Data, Integer, Boolean you would need to write 4 custom renderers. 因此,如果您具有String,Data,Integer,Boolean,则需要编写4个自定义渲染器。
See Table Row Rendering for an approach that will allow you to write the code once no matter haw many data types you have in the table. 请参阅表行渲染以获取一种方法,该方法将使您无论表中有多少数据类型,都可以立即编写代码。 This approach overrides the preparerrenderer(...)
method of the JTable. 此方法重写JTable的preparerrenderer(...)
方法。
As stated in Concepts: Editors and Renderers section of How to Use Tables tutorial, if you din not specify a renderer for a particular column then the table invokes the table model's getColumnClass
method to get a default renderer for the column type. 如“ 如何使用表”教程的“ 概念:编辑器和渲染器”部分所述,如果您未为特定列指定渲染器,则该表将调用表模型的getColumnClass
方法以获取该列类型的默认渲染器。
If you have overridden getColumnClass
method then your approach might not work as expected. 如果您重写了getColumnClass
方法,则您的方法可能无法按预期工作。 For instance: 例如:
DefaultTableModel model = new DefaultTableModel(new Object[]{"Column # 1", "Column # 2"}, 0) {
@Override
public Class<?> getColumnClass(int columnIndex) {
Class columnClass = Object.class;
switch(columnIndex) {
case 0: columnClass = String.class; break;
case 1: columnClass = Boolean.class; break;
}
return columnClass;
}
};
Then doing this: 然后这样做:
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {// your code here});
Won't work for the second column since the getColumnClass
method will return Boolean.class
and there's a default cell renderer for that class ( JCheckBox
). 由于getColumnClass
方法将返回Boolean.class
,因此不适用于第二列,并且该类具有默认的单元格渲染器( JCheckBox
)。
In your case I would suggest you override JTable.prepareRenderer() method instead to set rows background color independently of the renderer type ( JLabel
, JCheckBox
, or even custom renderers): 在您的情况下,我建议您重写JTable.prepareRenderer()方法,以独立于渲染器类型( JLabel
, JCheckBox
甚至是自定义渲染器)设置行背景色:
JTable table = new JTable(model) {
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
c.setBackground(row % 2 == 0 ? Color.LIGHT_GRAY : Color.WHITE);
return c;
}
};
Okay, scrap all of what I just wrote. 好吧,把我刚才写的所有东西都扔掉。
Attempt #2: 尝试2:
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 this;
}
});
You need to be returning the current object, not the reference of the super() call. 您需要返回当前对象,而不是super()调用的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.