By using:
jLabel1.getIcon().toString();
It returns only the reference to the object " javax.swing.ImageIcon@2982ccce
";
Not the absolute path, so how could I return the absolute path of that Icon?
For example: C:/File/imageicon.jpg
JLabel jLabel1 = new JLabel();
jLabel1.setIcon(new ImageIcon(getClass().getResource("/lk/edu/ijse/examsimulator/img/Hand With Pen_36px.png")));
if (jLabel1.getIcon() != null) {
Icon iconPath = jLabel1.getIcon();
System.out.println(iconPath.toString());
/* print
* file:/E:/Quick%20access/Documents/NetBeansProjects/ExamSimulator/ExmClientAdmin/build/classes/lk/edu/ijse/examsimulator/img/Hand%20With%20Pen_36px.png
*/
String absolutePath = iconPath.toString().replaceFirst("file:/", "");
absolutePath = absolutePath.replaceAll("%20", " ");
System.out.println(absolutePath);
/* print
*
* E:/Quick access/Documents/NetBeansProjects/ExamSimulator/ExmClientAdmin/build/classes/lk/edu/ijse/examsimulator/img/Hand With Pen_36px.png
*
* it's my image absolute path */
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.