繁体   English   中英

设置JTable中指定的单元格的背景色

[英]Set background color for a cell specified in JTable

我试图在JTable中定义单元格的颜色,并将行参数和列以及所需的颜色传递给color。 但是,他采用的实现方式将特定点绘制在整个列上。

    ...        
    table.getColumnModel().getColumn(2).
          setCellRenderer(new CellRenderer(3,2,new Color(24,65,87)));

class CellRenderer extends DefaultTableCellRenderer {

    private static final long serialVersionUID = 1L;
    private Integer row;
    private Integer col;
    private Color color;

    public CellRenderer(Integer row, Integer col, Color color) {
        this.row = row;
        this.col = col;
        this.color = color;
    }

    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int col) {

        Component c = super.getTableCellRendererComponent(table, value,
                isSelected, hasFocus, row, col);

        if(this.row == row && this.col == col){
            c.setBackground(this.color);
        }

        return c;
    }
}

单元格渲染器用于给定列或对象类型的所有单元格。 您需要提供一些对象来维护您要使用的信息,以便渲染器可以决定要突出显示的内容

或使用具有突出显示功能的SwingLabs SwingX库中的JXTable

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM