[英]How to change the color of a specific cell in a JTable (JAVA)?
well my question is how to change the color of a specific cell in a JTable in Java? 好吧,我的问题是如何在Java中更改JTable中特定单元格的颜色? as far as I know the first thing I should do is to override the method CellRendered I have already did this part as follows:
据我所知,我应该做的第一件事就是重写CellRendered方法,我已经按照以下步骤完成了这一部分:
public class CustomTableCellRenderer extends DefaultTableCellRenderer
{
int amount;
int f,c;
public CustomTableCellRenderer(int a)
{
amount = a;
}
public CustomTableCellRenderer()
{
}
@Override
public Component getTableCellRendererComponent
(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column)
{
Component cell = super.getTableCellRendererComponent
(table, value, isSelected, hasFocus, row, column);
if(amount == 3)
{
cell.setBackground(Color.LIGHT_GRAY);
}
if(amount == 1)
{
cell.setBackground(Color.cyan);
}
if(amount == 2)
{
cell.setBackground(Color.orange);
}
return cell;
}
} }
And when I want to change the color of the cell, I change the color but it changes the whole column, the part of the code where I used the override goes as follows: 当我想更改单元格的颜色时,我更改了颜色,但它更改了整个列,使用覆盖的代码部分如下:
Cache_table.getColumnModel().getColumn(columna).setCellRenderer(new CustomTableCellRenderer(1));
How can I specify the exact position of the cell I want to change the color, specifying the number of row and column: 如何指定要更改颜色的单元格的确切位置,并指定行数和列数:
Eg: 例如:
new CustomTableCellRenderer(int row, int column); 新的CustomTableCellRenderer(int row,int column);
Is that possible? 那可能吗?
Thanks guys¡¡ 多谢你们
Consider using else if
statements and then adding a default value in the default last else block. 考虑使用
else if
语句,然后在默认的last else块中添加默认值。
Also, and this is key , don't set amount in the renderer's constructor -- that won't work. 另外, 这是关键 ,不要在渲染器的构造函数中设置数量-这将无法工作。 Instead you must get the amount result inside of the
getTableCellRendererComponent
method, usually from the cell's value, or from the value from another model cell on the same row. 取而代之的是,您必须在
getTableCellRendererComponent
方法内部获取金额结果,通常是通过单元格的值或同一行中另一个模型单元格的值获得。
@Override
public Component getTableCellRendererComponent
(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererCo
// check that we're in the right column
if (column != correctColumn) {
// if not the right column, don't change cell
return cell;
}
// SomeType is the type of object held in the correct column
SomeType someType = (SomeType) value;
if (value == null) {
value = "";
return cell;
}
// and hopefully it has a method for getting the amount of interest
int amount = someType.getAmount();
if(amount == 3) {
cell.setBackground(Color.LIGHT_GRAY);
} else if(amount == 1) {
cell.setBackground(Color.cyan);
} else if(amount == 2) {
cell.setBackground(Color.orange);
} else {
cell.setBackground(null); // or a default background color
}
Also, you might need to be sure that your cell is opaque. 另外,您可能需要确保单元格是不透明的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.