未调用特定于类的渲染器组件 [英] Class specific renderer component not called

查看:117
本文介绍了未调用特定于类的渲染器组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've a JTable set to display String and Boolean values in the same column. I've the following piece of code to setup renderers for both the object types.

    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的渲染器总是被调用而不是布尔或字符串。我尝试删除Object的渲染器,但仍然没有运气。

The issue I face is that, the renderer for Object gets called always instead of Boolean or String. I tried removing the renderer for Object, still no luck.

推荐答案


我有一个JTable集在同一列中显示字符串和布尔值

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

然后你不能只使用普通的渲染逻辑。

Then you can't just use the normal rendering logic.

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

Normally the renderer is choosen based on the value returned by the getColumnClass(...) method. However, this is column based, not cell based so you won't know which renderer to return.

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

Instead you need to override the getCellRenderer(...) and getCellEditor(...) methods to return the renderer/editor based on the data in the cell.

下面给出了这种方法的一个例子:

An example of this approach is given below:

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

上面的代码只使用默认的String和Boolean渲染器和编辑。

The above code just uses the default String and Boolean renderers and editors.

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

The other approach would be to create custom renderers and editors so that each is aware of the two possible data types and returns the appropriate renderer/editor.

这篇关于未调用特定于类的渲染器组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆