【问题标题】:ImageIcon in JTableCellJTableCell 中的 ImageIcon
【发布时间】:2015-06-11 14:13:09
【问题描述】:

我在将 ImageIcon 添加到 JTable 中的 JLabel 时遇到问题。到目前为止,我完全能够根据单元格中数据的值来操作单元格,但是每当我尝试添加图像时,我只会看到文本。

表格渲染器

class DeviceTableModel extends AbstractTableModel {
    private Object[][] data = Globals.getArray();
    private String[] columnNames = {"Name","Status","Description"};


    @Override
    public int getRowCount() {
        return data.length;
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return data[rowIndex][columnIndex];
    }

    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }

    @Override
    public Class getColumnClass(int c) {
        return getValueAt(0,c).getClass();
    }

    @Override
    public void setValueAt(Object value, int row, int col) {
        data[row][col] = value;
        fireTableCellUpdated(row,col);
    }

}

这是我在 JTable 中使用的渲染器。

 @Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
    JLabel comp = (JLabel)super.prepareRenderer(renderer, row, col);
    Object value = getModel().getValueAt(row, col);

    if (value.equals("online")) {
        comp.setIcon(new ImageIcon("/Res/online.png"));
        comp.setBackground(Color.green);
    }else {
        comp.setBackground(Color.white);
    }

    return comp;
}

颜色和文字设置很好,但图标不会显示。任何想法将不胜感激!

EDIT-VGR 和 Camickr 的建议

您的建议很准确并解决了问题!看看重做的部分。我很感激。谢谢大家!

 //preloaded just added here to show. 
 ImageIcon icon = new  ImageIcon(getClass().getResource("/Res/onlineIcon.png"));


 @Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
    JLabel comp = (JLabel)super.prepareRenderer(renderer, row, col);
    Object value = getModel().getValueAt(row, col);

    if (value.equals("online")) {
        comp.setIcon(icon);
        comp.setBackground(new Color(173,255,92));
    }else {
        comp.setIcon(null);
        comp.setBackground(Color.white);
    }
    return comp;
}
}

【问题讨论】:

  • ImageIcon constructor documentation 清楚地表明字符串参数是一个文件名。除非您的系统在文件系统的根目录中有Res 目录,否则您可能打算使用new ImageIcon(getClass().getResource("/Res/online.jpg"))new ImageIcon(getClass().getResource("/online.jpg"))。请注意,您的 else 子句应将图标设置为 null,因为单个渲染器可能用于多个表格单元格。
  • 1) 代码是否正在执行?添加 println(...) 语句进行验证。 2) 正在读取图像吗?此外,在 prepareRenderer() 方法中不断读取图像也不是一个好主意。渲染代码应该很快。图片应该是预加载的。
  • 哦,你们太棒了。 @VGR 你在我调用资源的方式上是正确的! camickr 我听取了您的建议并预加载了图像,现在速度要快得多。你们真棒。 VGR 请将您的答案添加为真实答案,以便我接受!

标签: java swing jtable imageicon renderer


【解决方案1】:

ImageIcon constructor documentation 清楚地表明字符串参数是一个文件名。除非您的系统在文件系统的根目录中有一个Res 目录,否则您可能打算使用new ImageIcon(getClass().getResource("/Res/online.jpg"))new ImageIcon(getClass().getResource("/online.jpg"))

请注意,您的 else 子句应将图标设置为 null,因为单个渲染器可能用于多个表格单元格。

【讨论】:

    猜你喜欢
    • 2020-07-21
    • 2012-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    • 1970-01-01
    • 2013-05-14
    • 2012-11-27
    相关资源
    最近更新 更多