将图像插入 JTable
我在将图像插入 JTable 时遇到问题。我搜索了一下,发现有两种方法。您可以覆盖表模型的 getcolumnclass 或创建自定义 tablecellrender。
我选择使用以下代码制作自己的单元格渲染器:
public class MyRenderer extends DefaultTableCellRenderer {
/*
* @see TableCellRenderer#getTableCellRendererComponent(JTable, Object, boolean, boolean, int, int)
*/
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int column) {
ImageIcon icon = new ImageIcon("Images/green.png");
setText((String)value);
setIcon(icon);
return this;
}
}
在填写表格字段的方法中,我使用此行添加图像。
laneTable.getColumnModel().getColumn(4).setCellRenderer(new MyRenderer());
问题是图像显示为空白。它肯定会渲染一些东西,因为当您选择该行时,所有字段都会突出显示蓝色,除了第四列(带有图像的列)仍然是白色的。我一生都无法弄清楚为什么图像没有显示。我以各种方式调整了行高,看看图像是否在那里,但在视图之外或其他什么地方。看来并非如此。
我也尝试执行覆盖方法,但我想我没有正确执行语法,因为 netbeans 给出了语法错误,而且我还没有找到关于如何执行此操作的合适示例。
谢谢。
编辑:我尝试了更简单的方法,使用下面的代码覆盖该类,但单元格仍然是空白的。我已经测试过将图像插入到标签中并且这些工作正常,因此路径是正确的。
@Override
public Class getColumnClass(int column)
{
if (column == 4)
{
return ImageIcon.class;
}
return Object.class;
// other code; default to Object.class
}
I'm having an issue inserting images into a JTable. Ive searched and found there are 2 ways. You can either override the getcolumnclass of the tablemodel or create your custom tablecellrender.
I chose to make my own cell renderer with this code:
public class MyRenderer extends DefaultTableCellRenderer {
/*
* @see TableCellRenderer#getTableCellRendererComponent(JTable, Object, boolean, boolean, int, int)
*/
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int column) {
ImageIcon icon = new ImageIcon("Images/green.png");
setText((String)value);
setIcon(icon);
return this;
}
}
In the method where I fill in my table's fields, I am adding the images with this line..
laneTable.getColumnModel().getColumn(4).setCellRenderer(new MyRenderer());
The problem is that the images are showing up as blank white space. Its definitely rendering something because when you select the row, all of the fields highlight blue except for the 4th column(the one with the images) is still white. I cannot for the life of me figure out why the images are not showing. I've adjusted the row height in all sorts of ways to see if maybe the image was there, but out of view or something. Doesn't seem to be the case.
I also tried to do the override method but I guess I didn't do the syntax correctly because netbeans was giving syntax errors, and I have yet to find a decent example on how to do it.
Thanks.
edit: I've attempted the easier way, overriding the class using the code below but the cells are still blank. And I have tested inserting images into labels and those work, so the path is correct.
@Override
public Class getColumnClass(int column)
{
if (column == 4)
{
return ImageIcon.class;
}
return Object.class;
// other code; default to Object.class
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么要重新发明轮子呢?正如您所看到的,您遇到了问题,因此只需使用默认渲染器并覆盖 getColumnClass() 方法即可。
另外,您永远不会阅读红色代码中的图像。
更新:
也许这更容易理解:
Why reinvent the wheel? As you can see you are having problems, so just use the default renderer and override the getColumnClass() method.
Also, you would NEVER read the image in the rednering code.
Update:
Maybe this is easier to understand: