在 Java 中更改表格单元格颜色

发布于 2024-08-31 08:44:30 字数 174 浏览 1 评论 0 原文

我已经阅读并实现了这个 Changing JTable cell color

我想知道的是如何实际使用这个代码?我只想在单击表格单元格时更改它的颜色。

I have read and implemented this Changing JTable cell color

What I'd like to know is how to actually use this code? I just want to change a table cell's colour when I click on it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏雨凉 2024-09-07 08:44:30

在您引用的代码中,您有一个自定义的 CellRenderer。

将其添加到表后,您所需要做的就是在适当的位置进行格式化:

class CustomRenderer extends DefaultTableCellRenderer 
{
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
        Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        // Formatting here

        return c;
    }
}

DefaultTableCellRenderer 只不过是在 JTable 中使用的组件,用于绘制单元格。更准确地说,在本例中,该组件是一个 JLabel(您可以通过检查 DefaultTableCellRenderer 的源代码来看到这一点)。

因此,您应该做的所有格式化都在“c”对象上(或在“this”上,因为该方法实际上每次都返回相同的组件:其本身)。例如,c.setBackground()将允许您设置背景颜色。

将为 JTable 的每个单元格调用被重写的 getTableCellRendererComponent() 方法,并通过参数告诉您有关上下文的信息。您知道表格、行、列、应该显示的值,并且您还知道单元格是否被选择,这可能对您的情况有所帮助:

if (selected)
    c.setBackground(Color.YELLOW);

要更进一步,请注意,因为您重写了 DefaultTableCellRenderer类,并使用它自己的方法,您已经完成了一些格式化,例如背景颜色,这是表中的颜色。因此,您只需在需要时定义自己的颜色即可。如果没有,您将必须注意所有情况,因为由于使用了相同的组件,您将以颜色设置一次结束,然后应用于所有连续的单元格,因为不会执行任何操作来更改它。

如果您想了解有关它的完成和使用方式的更多信息,我建议您阅读 DefaultTableCellRenderer 的源代码(及其在 JTable 中的使用)。

In the code you refer to, you have a custom CellRenderer.

Once you added it to the table, all you need is to do the formatting in the appropriate place:

class CustomRenderer extends DefaultTableCellRenderer 
{
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
        Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        // Formatting here

        return c;
    }
}

A DefaultTableCellRenderer is nothing more or less than the component which will be used in the JTable, to paint the cells. To be more precise, in this case, the component is a JLabel (you can see this by checking sources from DefaultTableCellRenderer).

So all the formatting you should do is on the "c" object (or on "this", since the method actually returns the same component each time: itself). For example, c.setBackground() will allow you to set a background color.

The getTableCellRendererComponent() method which is overridden will be called for each cell of the JTable, with parameters telling you about the context. You know the table, the row, the column, the value which is supposed to be displayed, and you also know if the cell is selected or not, which could help with your case:

if (selected)
    c.setBackground(Color.YELLOW);

To go further, note that because you override the DefaultTableCellRenderer class, and use its own method, you have already some formatting done, like the background color, which is the one from the table. As such, you need only to define your own color when you need to. If not, you would have to take care about all cases, because since the same component is used, you would end with the color set once, and then applied to all consecutive cells, because nothing would have been done to change it.

I recommend you to read the sources from DefaultTableCellRenderer (and its uses in JTable), if you want to learn more about the way it is done and used.

若水微香 2024-09-07 08:44:30

这是否意味着单元格颜色永远改变,或者一旦您单击另一个单元格它就会重置。

如果您只想暂时更改颜色,那么最简单的方法是使用 表行渲染,因此您不必为每种类型的数据创建多个渲染器。

如果您希望单元格颜色是永久的,那么它会涉及更多,因为现在您实际上需要为每个应具有不同颜色的单元格保存数据。同样,最简单的方法是使用上面的方法,然后保留一组所有彩色单元格。

Does this mean the cell color changes forever, or does it reset once you click on another cell.

If you just want the color to change temporarily then the easiest way is to use the concepts presented in Table Row Rendering so you don't have to create multiple renderers for each type of data.

If you want the cell color to be permanent, then it is much more involved, because now you actually need to save the data for each cell that should be colored differently. Again the easiest approach is to use the approach from above and then maybe keep a Set of all the colored cells.

那伤。 2024-09-07 08:44:30

当我想为 JTable 中的特定单元格着色时,我也遇到了困难。
您可以创建自定义表格单元格渲染并将行/列作为参数发送:

class CustomRenderer extends DefaultTableCellRenderer {
    int col; 
    int row;
    public CustomRenderer (int col, int row) 
    {
       this.col = col;
       this.row = row;
    }
    public Component getTableCellRendererComponent
(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
        Component c = super.getTableCellRendererComponent
                          (table, value, isSelected, hasFocus, row, column);

        setForeground( (column == this.col && row == this.row) 
                                   ? Color.red : Color.black );

        return c;
    }
}

table.getColumnModel().getColumn(0).setCellRenderer(new CustomRenderer(0, 1);
table.getColumnModel().getColumn(1).setCellRenderer(new CustomRenderer(1, 3);

I struggled too when I wanted to color a particular cell in JTable.
You can create a custom table cell render and send row/col as params:

class CustomRenderer extends DefaultTableCellRenderer {
    int col; 
    int row;
    public CustomRenderer (int col, int row) 
    {
       this.col = col;
       this.row = row;
    }
    public Component getTableCellRendererComponent
(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
        Component c = super.getTableCellRendererComponent
                          (table, value, isSelected, hasFocus, row, column);

        setForeground( (column == this.col && row == this.row) 
                                   ? Color.red : Color.black );

        return c;
    }
}

table.getColumnModel().getColumn(0).setCellRenderer(new CustomRenderer(0, 1);
table.getColumnModel().getColumn(1).setCellRenderer(new CustomRenderer(1, 3);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文