gpt4 book ai didi

java - 获取前台单元格jtable

转载 作者:行者123 更新时间:2023-11-30 07:20:57 25 4
gpt4 key购买 nike

我有Jtable,有这样的记录 http://i.stack.imgur.com/TFqNB.jpg

我使用JTable.prepareRenderer(...)方法

但是后来我得到 getForeground 它的颜色是错误的,这是我的代码,所以出了什么问题??

JTable代码:

tb1_1 = new javax.swing.JTable(){
public Component prepareRenderer (TableCellRenderer renderer, int rowIndex, int columnIndex){
Component componenet = super.prepareRenderer(renderer, rowIndex, columnIndex);
if(rowIndex %2 == 0){
componenet.setForeground(Color.RED);
}else{
componenet.setForeground(Color.BLACK);
}
return componenet;
}
};

类获取颜色:

public class GetColor {

private int red;
private int green;
private int blue;
private Color color;

public GetColor(Color color) {
this.color = color;
}

public boolean isColorRed() {
return getRed() == 255 && getGreen() == 0 && getBlue() == 0;
}

private int getRed() {
return color.getRed();
}

private int getGreen() {
return color.getGreen();
}

private int getBlue() {
return color.getBlue();
}

}

最终我的代码返回isRed

int r = 2;
int cl = 3;
TableCellRenderer tcr = tb1_1.getCellRenderer(r, cl);
Component c = tcr.getTableCellRendererComponent(tb1_1, dtb.getValueAt(r, cl), false, false, r, cl);
GetColor gc = new GetColor(c.getForeground());
System.err.println(gc.isColorRed()); **// is will return false when get Cell have foreground color red**

最佳答案

您正在查看 default renderer 提供的前景色,

Component c = tcr.getTableCellRendererComponent(table, model.getValueAt(r, cl), false, false, r, cl);
System.out.println(c.getForeground());

通常是黑色。

java.awt.Color[r=0,g=0,b=0]

您可能想查看 prepareRenderer() 提供的颜色,

Component c = table.prepareRenderer(tcr, r, cl);
System.out.println(c.getForeground());

您将其设置为红色。

java.awt.Color[r=255,g=0,b=0]

关于java - 获取前台单元格jtable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37587624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com