Class 未调用特定渲染器组件

Class specific renderer component not called

我将 JTable 设置为在同一列中显示字符串和布尔值。我有以下代码来为这两种对象类型设置渲染器。

    table.setDefaultRenderer(Boolean.class, new BooleanHandler());
    table.setDefaultRenderer(String.class, new StringHandler());
    table.setDefaultRenderer(
            Object.class,
            new DefaultTableCellRenderer() {
                @Override
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                    System.out.println("Inside overridden function");
                    return super.getTableCellRendererComponent(table, value, isSelected, hasFocus,row, column);
                }
            }
    );

我面临的问题是,对象的渲染器总是被调用,而不是布尔值或字符串。我尝试删除 Object 的渲染器,仍然没有成功。

I've a JTable set to display String and Boolean values in the same column

那你不能只使用正常的渲染逻辑。

通常,渲染器是根据 return 通过 getColumnClass(...) 方法编辑的值来选择的。但是,这是基于列的,而不是基于单元格的,因此您不知道要使用哪个渲染器 return。

相反,您需要根据单元格中的数据将 getCellRenderer(...)getCellEditor(...) 方法覆盖为 return renderer/editor。

下面给出了这种方法的示例:

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class TablePropertyEditor extends JFrame
{
    public TablePropertyEditor()
    {
        String[] columnNames = {"Type", "Value"};
        Object[][] data =
        {
            {"String", "I'm a string"},
            {"Date", new Date()},
            {"Integer", new Integer(123)},
            {"Double", new Double(123.45)},
            {"Boolean", Boolean.TRUE}
        };

        JTable table = new JTable(data, columnNames)
        {
            private Class editingClass;

            public TableCellRenderer getCellRenderer(int row, int column)
            {
                editingClass = null;
                int modelColumn = convertColumnIndexToModel(column);

                if (modelColumn == 1)
                {
                    Class rowClass = getModel().getValueAt(row, modelColumn).getClass();
                    return getDefaultRenderer( rowClass );
                }
                else
                    return super.getCellRenderer(row, column);
            }

            public TableCellEditor getCellEditor(int row, int column)
            {
                editingClass = null;
                int modelColumn = convertColumnIndexToModel(column);

                if (modelColumn == 1)
                {
                    editingClass = getModel().getValueAt(row, modelColumn).getClass();
                    return getDefaultEditor( editingClass );
                }
                else
                    return super.getCellEditor(row, column);
            }

            //  This method is also invoked by the editor when the value in the editor
            //  component is saved in the TableModel. The class was saved when the
            //  editor was invoked so the proper class can be created.

            public Class getColumnClass(int column)
            {
                return editingClass != null ? editingClass : super.getColumnClass(column);
            }
        };

        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane( table );
        getContentPane().add( scrollPane );
    }

    public static void main(String[] args)
    {
        TablePropertyEditor frame = new TablePropertyEditor();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }
}

以上代码仅使用默认的字符串和布尔渲染器和编辑器。

另一种方法是创建自定义呈现器和编辑器,以便每个都知道两种可能的数据类型和 return 适当的 renderer/editor。