更改 JTable 单元格颜色

2022-01-12 00:00:00 rendering formatting java swing jtable

这简直把我逼疯了.

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

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.

这是我目前的设置:

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

setDefaultRenderer的第一个参数需要用什么?API 只是说类".我不知道该放什么.

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.

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

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.

推荐答案

setDefaultRenderer 的第一个参数中,将要覆盖的 Class 的 class literal渲染.即,如果您的数据包含所有字符串,则可以放置

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

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

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

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

最后,要在渲染器中更改背景颜色:

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

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

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

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();
    }
}

这样就可以为 IColorable.class 注册一个渲染器,并且不需要为每个实现注册一个单独的渲染器.

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

相关文章