首页 > 解决方案 > 如何获取 ImageIcon 路径 String Java Jtable

问题描述

if (j == model.getColumnCount() - 1) {
    JLabel excelJL = (JLabel) model.getValueAt(i, j);
    ImageIcon excelImageIcon = (ImageIcon) excelJL.getIcon();
    System.out.println(excelImageIcon.);
}

我有一个图像存储JLabel并显示JTable在行单元格中。

如何将图像路径作为字符串获取?

我能够检索图像并将其显示在另一个标签上,但我无法获得它的路径。

C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg

标签: javaswingpathjtableimageicon

解决方案


JLabel excelJL = (JLabel) model.getValueAt(i, j);
ImageIcon excelImageIcon = (ImageIcon) excelJL.getIcon();

//Image Name Is Stored In ImageIcons Description First set it And Then Retrieve it
String excelImagePath = excelImageIcon.getDescription();

我设法使用 getDescription() 方法获取图像路径。但首先您需要在将图像添加到 jtable 单元格时设置描述。


推荐阅读