获取前景单元格jtable

时间:2016-06-02 09:19:04

标签: java swing

我有像这样的记录的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;
    }
};

Class get Color:

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**

1 个答案:

答案 0 :(得分:1)

您正在查看docs here提供的前景色,

protected $menuItems = [
    'items' => [
        [
            'title' => 'test',
        ],
        $this->hasma() ? [
            'title' => 'Ma',
        ] : null,
    ],
]

通常是黑色。

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());