Java-是否可以在同一JTable单元格中放置图像和字符串? [英] Java - Is it possible to put an image and a String in the same JTable cell?

查看:67
本文介绍了Java-是否可以在同一JTable单元格中放置图像和字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何将字符串放入JTable单元中,并且知道如何将图像放入JTable单元中.但是是否可以将图像和字符串放入SAME JTable单元格中?

这样做的原因是我的JTable中有一个状态"列,此列目前包含绿色,琥珀色或红色图像.并且为了满足设计要求,我需要在每个图像旁边添加一些说明性文本(因此绿色图像旁边的文本将为在线",琥珀色图像旁边的文本将为未知",而下一个文本则为未知").到红色图像将是脱机").我需要在单列(或看起来/行为像单列)中而不是在两列中进行此操作.

我已经对此进行了研究,但根本找不到任何信息.

解决方案

是.

您需要使用自定义单元格渲染器.有关更多详细信息,请查看如何使用表. /p>

您实际上有两种选择,您可以简单地设置图标和单元格的文本,也可以使用渲染器工具提示文本...

public class IconTextCellRemderer extend DefaultTableCellRenderer {
    public Component getTableCellRendererComponent(JTable table,
                                  Object value,
                                  boolean isSelected,
                                  boolean hasFocus,
                                  int row,
                                  int column) {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        setText(...);
        setIcon(...);
        setToolTipText(...);
        return this;
    }
}

当然,您需要将渲染器应用于列...

TableColumnModel tcm = table.getColumnModel();
tcm.getColumn(x).setCellRenderer(new IconTextCellRemderer());

I know how to put a String into a JTable cell, and I know how to put an image into a JTable cell. But is it possible to put an image and a String into the SAME JTable cell?

The reason for this is that I have a 'status' column in my JTable, which at the moment contains either a green, amber or red image. And in order to fulfill the design requirement, I need to add some explanatory text alongside each image (so the text next to the green image would be "Online", the text next to the amber image would be "Unknown" and the text next to the red image would be "Offline"). I need to do this in a single column (or what looks/behaves like a single column) rather than two columns.

I have researched this, but found no info at all.

解决方案

Yes.

You need to use a custom cell renderer. Check out How to use Tables for more details.

You actually have two choices, you could simply set the icon and the text of the cell or you could use the renderers tooltip text instead...

public class IconTextCellRemderer extend DefaultTableCellRenderer {
    public Component getTableCellRendererComponent(JTable table,
                                  Object value,
                                  boolean isSelected,
                                  boolean hasFocus,
                                  int row,
                                  int column) {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        setText(...);
        setIcon(...);
        setToolTipText(...);
        return this;
    }
}

Of course you need to apply the renderer to the column...

TableColumnModel tcm = table.getColumnModel();
tcm.getColumn(x).setCellRenderer(new IconTextCellRemderer());

这篇关于Java-是否可以在同一JTable单元格中放置图像和字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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