在 Java 中更改表格单元格颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2780573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change table cell color in Java
提问by pypmannetjies
I have read and implemented this Changing JTable cell color
我已经阅读并实现了这个改变 JTable 单元格颜色
What I'd like to know is how to actually use this code? I just want to change a table cell's colour when I click on it.
我想知道的是如何实际使用此代码?我只想在单击时更改表格单元格的颜色。
回答by Gnoupi
In the code you refer to, you have a custom CellRenderer.
在您引用的代码中,您有一个自定义 CellRenderer。
Once you added it to the table, all you need is to do the formatting in the appropriate place:
将其添加到表格后,您只需在适当的位置进行格式化:
class CustomRenderer extends DefaultTableCellRenderer
{
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
// Formatting here
return c;
}
}
A DefaultTableCellRenderer is nothing more or less than the component which will be used in the JTable, to paint the cells. To be more precise, in this case, the component is a JLabel (you can see this by checking sources from DefaultTableCellRenderer).
DefaultTableCellRenderer 只不过是将在 JTable 中用于绘制单元格的组件。更准确地说,在这种情况下,该组件是一个 JLabel(您可以通过检查来自 DefaultTableCellRenderer 的源来看到这一点)。
So all the formatting you should do is on the "c" object (or on "this", since the method actually returns the same component each time: itself). For example, c.setBackground()will allow you to set a background color.
所以你应该做的所有格式化都是在“c”对象上(或在“this”上,因为该方法实际上每次都返回相同的组件:它自己)。例如,c.setBackground()将允许您设置背景颜色。
The getTableCellRendererComponent()method which is overridden will be called for each cell of the JTable, with parameters telling you about the context. You know the table, the row, the column, the value which is supposed to be displayed, and you also know if the cell is selected or not, which could help with your case:
getTableCellRendererComponent()将为 JTable 的每个单元格调用被覆盖的方法,参数告诉您上下文。您知道表格、行、列、应该显示的值,并且您还知道是否选择了单元格,这可能对您的情况有所帮助:
if (selected)
c.setBackground(Color.YELLOW);
To go further, note that because you override the DefaultTableCellRenderer class, and use its own method, you have already some formatting done, like the background color, which is the one from the table. As such, you need only to define your own color when you need to. If not, you would have to take care about all cases, because since the same component is used, you would end with the color set once, and then applied to all consecutive cells, because nothing would have been done to change it.
更进一步,请注意,因为您覆盖了 DefaultTableCellRenderer 类并使用了它自己的方法,所以您已经完成了一些格式设置,例如背景颜色,它是表格中的颜色。因此,您只需在需要时定义自己的颜色。如果不是,您将不得不处理所有情况,因为由于使用了相同的组件,您将以设置一次颜色结束,然后应用到所有连续的单元格,因为不会对其进行任何更改。
I recommend you to read the sources from DefaultTableCellRenderer (and its uses in JTable), if you want to learn more about the way it is done and used.
我建议您阅读 DefaultTableCellRenderer 的源代码(及其在 JTable 中的用途),如果您想了解更多有关它的完成和使用方式的信息。
回答by camickr
Does this mean the cell color changes forever, or does it reset once you click on another cell.
这是否意味着单元格颜色会永远改变,或者一旦您单击另一个单元格它会重置。
If you just want the color to change temporarily then the easiest way is to use the concepts presented in Table Row Renderingso you don't have to create multiple renderers for each type of data.
如果您只想临时更改颜色,那么最简单的方法是使用表格行渲染中介绍的概念,这样您就不必为每种类型的数据创建多个渲染器。
If you want the cell color to be permanent, then it is much more involved, because now you actually need to save the data for each cell that should be colored differently. Again the easiest approach is to use the approach from above and then maybe keep a Set of all the colored cells.
如果您希望单元格颜色是永久性的,那么它就复杂得多,因为现在您实际上需要为每个应该以不同颜色着色的单元格保存数据。同样,最简单的方法是使用上面的方法,然后保留一组所有彩色单元格。
回答by catalinp
I struggled too when I wanted to color a particular cell in JTable. You can create a custom table cell render and send row/col as params:
当我想为 JTable 中的特定单元格着色时,我也很挣扎。您可以创建自定义表格单元格渲染并将行/列作为参数发送:
class CustomRenderer extends DefaultTableCellRenderer {
int col;
int row;
public CustomRenderer (int col, int row)
{
this.col = col;
this.row = row;
}
public Component getTableCellRendererComponent
(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
Component c = super.getTableCellRendererComponent
(table, value, isSelected, hasFocus, row, column);
setForeground( (column == this.col && row == this.row)
? Color.red : Color.black );
return c;
}
}
table.getColumnModel().getColumn(0).setCellRenderer(new CustomRenderer(0, 1);
table.getColumnModel().getColumn(1).setCellRenderer(new CustomRenderer(1, 3);

