java - 未调用特定于类的渲染器组件

标签 java swing jtable tablecellrenderer

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

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

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

最佳答案

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

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

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

相反,您需要重写 getCellRenderer(...)getCellEditor(...) 方法以根据单元格中的数据返回渲染器/编辑器.

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

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

上面的代码仅使用默认的字符串和 boolean 渲染器和编辑器。

另一种方法是创建自定义渲染器和编辑器,以便每个渲染器和编辑器都知道两种可能的数据类型并返回适当的渲染器/编辑器。

关于java - 未调用特定于类的渲染器组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33216409/

相关文章:

java - 如何让我的 Action 监听器与我的计算方法进行通信,以便将文本输出到我的 GUI?

java - 为什么我的 Java Swing 窗口没有显示出来?

java - JScrollPane 上的单杠

java - 根据数据库值动态加载jTable中的Image

java.lang.ArrayIndexOutOfBoundsException : 0 >= 0 in swing

java - 过滤后的组合框中的文本编辑器不显示文本

java - 使用gson将json转换为包含模板变量的类的对象

java - SDN4 枚举到字符串的转换失败

java - 如何在 Android 上调整 Yuv 图像大小

java - 在 Swing 面板上排列 Swing 组件