Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I specify the icon's path if icon doesn't have a specified address?

I have a frame and want to set an icon for it. I use this code : JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLayout(null); f.setTitle("add icon example"); f.setBounds(200,200,200,200); Image icon = Toolkit.getDefaultToolkit().getImage("D:\\icon.png"); f.setIconImage(icon); f.setVisible(true); In this code , the address of image is specific , but what can I do if image and jar file are in a zip file and icon will be with jar file. I think I can write a code to unzip the file and then save the image in a specified address and then use it. But , please anyone help me to do it. Thanks.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If you’re trying to load an image that’s within a JAR, this is the most simple approach to this problem. This will also work if your jar is executed from within a zip archive.

        BufferedImage image = ImageIO.read(MyClass.class.getClassLoader().getResourceAsStream("image_name.png"));

Where MyClass is the name of the class you’re calling this code from.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading