如何将图像渲染到JTable单元格 [英] How to render an image to a JTable cell

查看:144
本文介绍了如何将图像渲染到JTable单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的JTable的单元格上应用渲染器,为此我创建了一个名为myRenderer的类:

I want to apply a renderer on a cell of my JTable, to do so I created a class named myRenderer :

import java.awt.Component;

import javax.swing.ImageIcon;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

public class MyRenderer extends DefaultTableCellRenderer {

public Component getTableCellRendererComponent(JTable table, ImageIcon icon) {

setIcon(icon);
return this;
}
}

我使用这段代码来应用渲染器在单元格上:

And I use this piece of code to apply the renderer on the cell :

MyRenderer renderer = new MyRenderer();

renderer.getTableCellRendererComponent(table, icon);

table.getColumnModel().getColumn(6).setCellRenderer(renderer);

问题是rebderer应用于第6列中的所有单元格,我想要它仅适用于一个单元格(行/列),但我不知道该怎么做?

The problem is that the rebderer is applied on all the cells in the column 6, and I want it to be applied on one cell only (row/column) but I don't know how to do so ?

提前致谢

推荐答案

除了你甚至没有正确覆盖 getTableCellRendererComponent 方法之外,你甚至不需要自定义渲染器以在列中显示图像

Besides the fact that you are not even properly overriding the getTableCellRendererComponent method, you don't even need a custom renderer to display an image in a column

来自如何使用表。以下是具有默认预配置渲染器的类型列表

From How to Use Tables. Here's a list of types with default pre-configured renderers


  • 布尔值 - 使用一个复选框。

  • 数字 - 由右对齐标签呈现。

  • Double,Float - 与Number相同,但是对象到文本的转换由NumberFormat实例执行(使用当前语言环境的默认数字格式)。

  • 日期 - 由标签呈现,由DateFormat实例执行对象到文本的转换(使用日期和时间的简短样式) )。

  • ImageIcon ,图标 - 由居中标签呈现。

  • Object - 由显示对象字符串值的标签呈现。

  • Boolean — rendered with a check box.
  • Number — rendered by a right-aligned label.
  • Double, Float — same as Number, but the object-to-text translation is performed by a NumberFormat instance (using the default number format for the current locale).
  • Date — rendered by a label, with the object-to-text translation performed by a DateFormat instance (using a short style for the date and time).
  • ImageIcon, Icon — rendered by a centered label.
  • Object — rendered by a label that displays the object's string value.

所以你可以放在表中添加 ImageIcon ,如果正确覆盖 getColumnClass()

So you can put add an ImageIcon to the table and it will be rendered as such, given you properly override the getColumnClass()

同样来自教程:

要选择显示列中单元格的渲染器,表格首先确定是否为该特定列指定了渲染器。如果没有,则表调用表模型的 getColumnClass 方法,该方法获取列单元格的数据类型。接下来,该表将列的数据类型与已注册单元格渲染器的数据类型列表进行比较

To choose the renderer that displays the cells in a column, a table first determines whether you specified a renderer for that particular column. If you did not, then the table invokes the table model's getColumnClass method, which gets the data type of the column's cells. Next, the table compares the column's data type with a list of data types for which cell renderers are registered

所以说你有一个 DefaultTableModel 有三列,你想要在最后一列中有一个 ImageIcon 。你会做这样的事情

So say you have a DefaultTableModel with three columns and you want an ImageIcon in the last column. You would do something like this

DefaultTableModel model = new DefaultTableModel(...) {
    @Override
    public Class<?> getColumnClass(int column) {
        switch (column) {
            case 2: return ImageIcon.class;
            default: return String.class
        }
    }
};
JTable table = new JTable(model);

然后只需将 ImageIcon 添加到第三列,它将如此呈现

Then by just adding an ImageIcon to the third column, it will be rendered as such

String colOneDate = "Data";
String colTwoData = "Data";
ImageIcon colThreeIcon = new ImageIcon(...);
model.addRow(new Object[] { colOneData, colTwoData, colThreeIcon });

您可能还需要相应地将列宽和高度设置为图像的大小。为此,您可以看到任何这些问题

You will probably also want to set the column widths and height accordingly, to the size of the image. For that you can see any of these questions

这篇关于如何将图像渲染到JTable单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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