简体   繁体   English

如何更改特定表格 header 的背景颜色和 JTable 中特定列的字体颜色?

[英]How to change the background color of a specific table header and font color of a specific column in a JTable?

Is there a way to change the background color of the 3 circled table headers of a JTable ?有没有办法改变JTable的 3 个带圆圈的表头的背景颜色?

I also want to change the font color of all the 3 circled columns to red.我还想将所有 3 个带圆圈的列的字体颜色更改为红色。

An image of my table is down below.我的桌子的图像在下面。 Thanks.谢谢。

https://i.stack.imgur.com/Igr2n.png

The way I know of is to create a TableCellRenderer that will handle the coloring of the foreground and background color.我知道的方法是创建一个TableCellRenderer来处理前景色和背景色的着色。

You could create a simple coloring scheme where the color doesn't change.您可以创建一个简单的配色方案,其中颜色不会改变。 You often see this in tables with alternating colored rows.您经常在具有交替颜色行的表格中看到这一点。 You could also use it for "conditional formatting".您也可以将其用于“条件格式”。 For example, highlight a cell if a value falls under a certain value.例如,如果某个值低于某个值,则突出显示一个单元格。 With a cell renderer, you will have better control of this.使用单元格渲染器,您将可以更好地控制这一点。

Here is a link to Oracle's tutorial on renderers .这是Oracle 渲染器教程的链接。

Lastly, since your question specifically asks to do this on specific cells, the link documentation states that最后,由于您的问题特别要求在特定单元格上执行此操作,因此链接文档指出

To specify a cell-specific renderer , you need to define a JTable subclass that overrides the getCellRenderer method.要指定特定于单元格的渲染器,您需要定义一个覆盖 getCellRenderer 方法的 JTable 子类。 For example, the following code makes the first cell in the first column of the table use a custom renderer :例如,以下代码使表格第一列中的第一个单元格使用自定义渲染器

TableCellRenderer weirdRenderer = new WeirdRenderer();
table = new JTable(...) {
    public TableCellRenderer getCellRenderer(int row, int column) {
        if ((row == 0) && (column == 0)) {
            return weirdRenderer;
        }
        // else...
        return super.getCellRenderer(row, column);
    }
};

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

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