I have created a jar
file for my GUI
application using IntelliJ. However, when I run the file, I cannot see the toolbar
which contains the buttons with ImageIcon
for different functionaltites. On looking around over stackoverflow (like here: Java Swing ImageIcon, where to put images?), I found that the file path for the images could be an issue. At present I am using the following code:
OpenImagingFileButton = new JButton(createImageIcon(currentPath.concat("/src/main/java/uni/images/open-file-icon-img.png")));
The currentPath
variable is obtained using this:
final String currentPath = currentRelativePath.toAbsolutePath().toString();
And the createImageIcon
method has the following code:
/**
* Returns an ImageIcon, or null if the path was invalid.
*/
protected static ImageIcon createImageIcon(String path) {
ImageIcon toolBarImage = new ImageIcon(path);
Image image = toolBarImage.getImage(); // transform it
Image newImg = image.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH); // scale it the smooth way
toolBarImage = new ImageIcon(newImg); // transform it back
return toolBarImage;
}
This code works perfectly when I run it in intelliJ, however, the toolbar is not visible when I run the jar
. I also tried moving the images folder directly under src
folder and then doing this:
ImageIcon(this.getClass().getResource("/images/open-file-icon-img.png"));
But this gives me aNullPointerException
. Where am I going wrong?