1

I'm trying to create one simple GUI based testing tool in Java using Eclipse. I have been trying to add icon to my application. Images are present inside the project in a folder. Could you please suggest what mistake I'm doing.

I'm used below two ways, unfortunately both are not helping me. -

1.) frame.setIconImage(image).

2.) setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource(Filepath)));

Below is the setup of my project in Eclipse -

enter image description here

Below is code which i'm using -

public static void main(String[] args) {

    JFrame frame = new JFrame("Testing Tool");

    // setting close operation
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // sets 500 width and 600 height
    frame.setSize(500, 600);

    try {
           Image image = new ImageIcon("/Project_T/Images/biplane.jpg").getImage();
           frame.setIconImage(image);


        } catch(Exception e) {
           System.out.println("Application icon not found");
        }
     // uses no layout managers
    frame.setLayout(null);

    // makes the frame visible
    frame.setLocationRelativeTo(null);  
    frame.setVisible(true);

}

Since i'm going to create an .exe file for this project using lunach4J, is this the way of keeping files (placing them in a folder of project since I would be using multiple images and files) that ensures the application running on any machine.

deHaar
  • 17,687
  • 10
  • 38
  • 51
Deepak Yadav
  • 321
  • 1
  • 8
  • 19
  • I think there are two main options: (1) having the image resources as project resources that stay in the jar and loading them via `ClassLoader` or (2) outputting the images into the folder of the jar or a subfolder and loading them from there at runtime. – deHaar Jul 04 '18 at 07:10

2 Answers2

0

This is the code I used with a Swing application packaged in an executable JAR file. The JFrame has the icon image.

private static final String APP_ICON_PATH = "myapp/icondirectory/report.png";
ImageIcon icon = new ImageIcon(ClassLoader.getSystemResource(APP_ICON_PATH));
frame.setIconImage(icon.getImage())

The "myapp" is the root of my package structure; e,g., the GUI class using the frame is in the package myapp.gui package. The image PNG files are within the application's executable JAR file (as you see in the code within a separate folder).

Also, look at the Oracle's Java Swing tutorials explain the usage of icons.

prasad_
  • 12,755
  • 2
  • 24
  • 36
  • I accept this answer, it is same as the what I have found. Thanks a lot. Most important is the package structure which needs to be understood so that we files are available during execution. – Deepak Yadav Jul 06 '18 at 06:49
0

I fiddled a lot and finally found a way out , since my explanation was a bit long so I thought of writing an answer. Also please suggest that is this a good way since we would be exporting our project.

Java gives you the power to change the ICON for your application with the help of setIconImage(image), but this method is not that direct, since we are dealing with multiple files those should be present while executing the code so we would have to ensure that their paths are correct and accessible in order to run smoothly.

Hence we save our files inside the src by creating another folder and from there we can import the files easily. Follow these steps

Step - 0 - Place the files inside src folder of the project

Put the files inside the scr folder by creating another folder, here I created another folder by the name of images and placed my files there, hence the file path would be

"Images/biplane.png",

please ensure that there are no "/" placed before Images (our folder name)

enter image description here

Step - 1 - Place the file path inside a URL variable

Step - 2(Optional) - Print this URL variable to cross check that this is not null. If this is null check your path again and repeat the activity so that this value is not null.

Step - 3 - Now pass this URL to ImageIcon method.

Step - 4 - Call the setIconImage method with that image.

Code Used -

URL url = test.class.getClassLoader().getResource("Images/biplane.png");
System.out.println("Path for file is :- \"" + url + "\"");

Image image = new ImageIcon(url).getImage();
frame.setIconImage(image);
Deepak Yadav
  • 321
  • 1
  • 8
  • 19