如何在java swing中获取要在JTable中显示的图像

时间:2023-02-09 14:48:43

I have created a jTable and have put an ImageIcon objects in one of the columns. I would like to know how I can get it to display the image rather than the image name as a string.

我创建了一个jTable并将ImageIcon对象放在其中一列中。我想知道如何让它显示图像而不是图像名称作为字符串。

Thanks

2 个解决方案

#1


You have to implement a TableCellRenderer and set it for the respective column.

您必须实现TableCellRenderer并为相应的列设置它。

A TableCellRenderer has just one method which returns a Component to display in a table cell. However, you probably need to adjust the row heights when you want to display images, since the rows won't adjust automatically to their content.

TableCellRenderer只有一个方法,它返回一个要在表格单元格中显示的Component。但是,您可能需要在要显示图像时调整行高,因为行不会自动调整其内容。

For how to use custom renderers you can refer to the Java Tutorials.

有关如何使用自定义渲染器的信息,请参阅Java教程。

#2


You need to provide a custom table cell renderer to show the image. You will find examples if you Google jtable image renderer. See here for one.

您需要提供自定义表格单元格渲染器以显示图像。如果您使用Google jtable图像渲染器,您会找到示例。在这里看一个。

#1


You have to implement a TableCellRenderer and set it for the respective column.

您必须实现TableCellRenderer并为相应的列设置它。

A TableCellRenderer has just one method which returns a Component to display in a table cell. However, you probably need to adjust the row heights when you want to display images, since the rows won't adjust automatically to their content.

TableCellRenderer只有一个方法,它返回一个要在表格单元格中显示的Component。但是,您可能需要在要显示图像时调整行高,因为行不会自动调整其内容。

For how to use custom renderers you can refer to the Java Tutorials.

有关如何使用自定义渲染器的信息,请参阅Java教程。

#2


You need to provide a custom table cell renderer to show the image. You will find examples if you Google jtable image renderer. See here for one.

您需要提供自定义表格单元格渲染器以显示图像。如果您使用Google jtable图像渲染器,您会找到示例。在这里看一个。