简体   繁体   中英

How to get the absolute path from an JLabel ImageIcon?

I am creating an Photo and Video Viewer. Already made the video part, now I am making the photo part.

Trying to get the absloute path, for something like this:

图片

And then it deletes. I'm using a JLabel for displaying. What i tried was:

System.out.println("Deleting file");

ImageIcon img = (ImageIcon) showDisplay.getIcon();
System.out.println(img.toString());

File file = new File(img.toString());
int response = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete the file " + file.getName() + "?");
if(response == JOptionPane.YES_OPTION) {
    deleteFile(file);
    //the thing that deleteFile has is file.delete();
}

But this returns javax.swing.ImageIcon@6aa93db7 in the console, not the absolute path.

Can someone help me here?

You can use the setDescription(...) and getDescription(...) methods for this.

Depending on your you load the Icon this may already contain the file path.

If not, then you can manually set this value yourself when you create the image and create the Icon.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM