Java JTable TableCellRenderer与ImageIcon列 [英] Java JTable TableCellRenderer With ImageIcon Column

查看:78
本文介绍了Java JTable TableCellRenderer与ImageIcon列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有自定义表模型的表,该表模型有两列.列0是ImageIcon类,列1是String类.

I have a table with a custom table model which has two columns. Column 0 is an ImageIcon class, and Column 1 is a String class.

public Class<?> getColumnClass(int col) {
    if (col == 0) {
        return ImageIcon.class;
    } else {
        return String.class;
    }
}

当我定义要添加到列中的新TableCellRenderer类以便对单元格进行样式设置时,它将覆盖ImageIcon类并将其设置为String.

When I define a new TableCellRenderer class to be added to the columns so I can style the cells, it overwrites the ImageIcon class and sets it to a String.

public class CustomTableCellRenderer extends DefaultTableCellRenderer
{
    public Component getTableCellRendererComponent (JTable table, Object obj, boolean isSelected,     boolean hasFocus, int row, int 
    column)
    {
    Component cell = super.getTableCellRendererComponent(table, 
      obj, isSelected, hasFocus, row, column);
    if(isSelected)
     cell.setBackground(Color.BLUE);
    return cell;
    }
}

关于如何解决此问题的任何想法?

Any ideas on how to fix this?

我的错误,某种程度上是隐藏的:

My mistake, it is sort of hidden:

当我定义要添加到列中的新TableCellRenderer类以便对单元格进行样式设置时,它将覆盖ImageIcon类并将其设置为String.

When I define a new TableCellRenderer class to be added to the columns so I can style the cells, it overwrites the ImageIcon class and sets it to a String.

所以问题在于,当我定义此TableCellRenderer类以对表进行样式设置时,表中的ImageIcon列将变为"File:..."之类的字符串,而不是实际的图标.

So the problem is that, when I define this TableCellRenderer class to style my table, the ImageIcon columns in my table turn to Strings like "File:..." instead of the actual icon.

推荐答案

无需创建自定义渲染器. JTable allready支持包含Icon的列的默认渲染器.您需要做的就是重写您似乎正在执行的getColumnClass()方法.

There is no need to create a custom renderer. JTable allready supports a default renderer for columns containing an Icon. All you need to do is override the getColumnClass() method, which you appear to be doing.

这篇关于Java JTable TableCellRenderer与ImageIcon列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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