I have JLabel in a frame that I want to have different images when I click on different buttons. This was easy to achieve. Here is the code
ImageIcon icon = new ImageIcon (img); icon.getImage().flush(); shopBanner.setIcon(icon);
Problem i0, s I used to provide the full path to the image, for example, C: \ Documents \ xxx. Now, when I tried the jar on another computer, I noticed that the images were not being used, this was obvious since the assigned path does not exist on the other computer.
In the project, I have 2 packages, one for images called images and another for source files called smartshopping. I tried using a couple of codes, but could not call the image from the "image" package. Please help me solve the problem. The project works fine on my computer if I provide the full path as C: / Docs ....
Here is the code
Image img = ImageIO.read(getClass().getResource("images/bb-banner-main.jpg")); ImageIcon icon = new ImageIcon (img); icon.getImage().flush(); shopBanner.setIcon(icon);
I even tried
URL img= this.getClass().getResource("images/icon.png"); ImageIcon imageIcon = new ImageIcon(img);
Nothing is working at the moment. What am I doing wrong. An image package is called images.
source share