Referencing a bufferedImage on a jLabel within a .jar file

By | October 5, 2018

I created a .jar file from Eclipse for a project.

Before the .jar creation, I was reading in images like this:

jLabel = new JLabel(new ImageIcon("src/someImage.png"));

But now, for the .jar, I’m using:

BufferedImage img ="someImage.png"));
jLabel = new JLabel(new ImageIcon(img));

Which works fine, however further in the code, I need to pick between particular images, and the non-.jar file used this:

icon = jLabel.getIcon().toString();
if (icon.contains("happy")) {
// do something

But obviously this won’t work. The icon as is comes out just as something like javax.swing.ImageIcon@35083305

I tried just adding something like jLabel.setText("someText"); but I have been unable to retrieve said text.

I saw this, and note that BufferedImage doesn’t have a method to get its filename (as of course it may not be a file).

Is there a way I can reference the jLabel in a similar fashion?


Leave a Reply

Your email address will not be published. Required fields are marked *