简体   繁体   English

用Java更改表格单元格颜色

[英]Change table cell color in Java

I have read and implemented this Changing JTable cell color 我已经阅读并实现了这种Changing JTable单元格颜色

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. 我只想在点击它时更改表格单元格的颜色。

In the code you refer to, you have a custom CellRenderer. 在您引用的代码中,您有一个自定义的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. DefaultTableCellRenderer只是在JTable中用于绘制单元格的组件。 To be more precise, in this case, the component is a JLabel (you can see this by checking sources from DefaultTableCellRenderer). 更确切地说,在这种情况下,组件是JLabel(您可以通过检查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). 所以你应该做的所有格式都是在“c”对象上(或“this”,因为该方法实际上每次返回相同的组件:本身)。 For example, c.setBackground() will allow you to set a background color. 例如, c.setBackground()将允许您设置背景颜色。

The getTableCellRendererComponent() method which is overridden will be called for each cell of the JTable, with parameters telling you about the context. 将为getTableCellRendererComponent()每个单元调用被覆盖的getTableCellRendererComponent()方法,并使用参数告知您上下文。 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. 更进一步,请注意,因为您重写了DefaultTableCellRenderer类并使用了自己的方法,所以您已经完成了一些格式化操作,例如背景颜色,即表格中的颜色。 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. 我建议你阅读DefaultTableCellRenderer中的源代码(及其在JTable中的用法),如果你想了解更多关于它的完成和使用方式的信息。

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. 同样最简单的方法是使用上面的方法,然后可以保留一组所有彩色单元格。

I struggled too when I wanted to color a particular cell in JTable. 当我想在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);

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

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