Java 更改 JTable 单元格颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/818287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:50:28  来源:igfitidea点击:

Changing JTable cell color

javaswingformattingrenderingjtable

提问by Josh Leitzel

This is driving me absolutely insane.

这让我完全疯了。

I know that, to change the formatting of table cells with JTable, I have to use my own renderer. But I cannot seem to implement this properly.

我知道,要使用 JTable 更改表格单元格的格式,我必须使用我自己的渲染器。但我似乎无法正确实施。

This is my current setup:

这是我目前的设置:

public class MyClass
{
    public static void main(String args[])
    {
        JTable myTable = new JTable(10, 10);
        myTable.setDefaultRenderer ([I dont know what to put here], new CustomRenderer());
    }
}

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
        return c;
    }
}

What do I need to use for the first parameter of setDefaultRenderer? The API just says 'class'. I have no idea what to put there.

我需要为 的第一个参数使用setDefaultRenderer什么?API 只是说“类”。我不知道该放什么。

Could someone just explain, in the simplest of terms, how I go about implementing this? Please provide an example of how I can change the formatting from within the main()method as well.

有人可以用最简单的术语解释我如何实现这一点吗?请提供一个示例,说明如何从main()方法中更改格式。

采纳答案by Camilo Díaz Repka

In the first parameter for setDefaultRenderer, put the class literalfor the Class that you want to override rendering. I.e., if your data consist all of strings, you can put

在 for 的第一个参数中setDefaultRenderer,放置要覆盖呈现的类的类文字。即,如果您的数据由所有字符串组成,您可以将

myTable.setDefaultRenderer(String.class, new CustomRenderer());

If your data also consists of values with BigDecimalor Integeras classes, you have to invoke that method several times for each class type (BigDecimal.classor Integer.classin each case).

如果您的数据还包含具有类BigDecimalInteger作为类的值,则您必须为每个类类型(BigDecimal.classInteger.class在每种情况下)多次调用该方法。

And finally, to change the background color you do this in your renderer:

最后,要更改背景颜色,请在渲染器中执行以下操作:

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);
        c.setBackground(new java.awt.Color(255, 72, 72));
        return c;
    }
}

If you write a renderer that should work for all classes of an interface, you will also need to modify the getColumnClassfunction of your table modeland let it return the interface class for all objects that implement this interface:

如果您编写的渲染器应该适用于接口的所有类,您还需要修改表模型getColumnClass函数并让它返回实现此接口的所有对象的接口类:

public Class<? extends Object> getColumnClass(int c) {
    Object object = getValueAt(0, c);
    if(object == null) {
        return Object.class;
    if(getValueAt(0, c) instanceof IColorable) {
        return ICarPart.class;
    } else {
        return getValueAt(0, c).getClass();
    }
}

With that one can register a renderer for IColorable.class and does not need to register a separate renderer for each implementation.

有了它,可以为 IColorable.class 注册一个渲染器,而无需为每个实现注册一个单独的渲染器。