H3AP
H3AP

Reputation: 1058

Eclipse refer to image

So i am new to Java and this will probaly the easiest question you will ever see, still I can not find the anser on the internet.

I want to set the icon of my program, This code does works: frame.setIconImage(Toolkit.getDefaultToolkit().getImage("MYPROBLEM"));

Yet i can not get the path correct at the MYPROBLEM section. This is my structure:

Projectname

-src

--default package

---myfunctions

--test <----a map

---icon.png

Whenever i replace MYPROBLEM with src/test/icon.png it does work. however when i export my application as jar the default Java icon shows up. Replacing MYPROBLEM with something like test/icon.png does also not works.

I do appologise for my English. Bear with me because I am a newbie ;)

Upvotes: 1

Views: 188

Answers (2)

clav
clav

Reputation: 4251

This will load an image from the classpath.

Image image = new ImageIcon(this.getClass().getResource("MYPROBLEM")).getImage();
frame.setIconImage(image);

Upvotes: 1

mohamed nur
mohamed nur

Reputation: 331

for some reason sometimes it does not work but what you can do is to add the full path of the location of the image. here is an example

import java.awt.Dimension;
import java.awt.Image;

import java.awt.Toolkit;

import javax.swing.JFrame;

import java.awt.Toolkit;

import javax.swing.JFrame;

public class JFrameIcon {

    public static void main(String[] args) {

        JFrame frame = new JFrame();

        frame.setSize(300, 150);

        frame.setTitle("tutorialData.com");

        frame.setResizable(false);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setAlwaysOnTop(true);

        frame.setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\Sirnurpc\\Desktop\\icon.png"));

        frame.setVisible(true);

    }

}

reference seticon

In your case if you are using Netbeans then the location would be

C:\Users\yourloginname\Documents\NetBeansProjects\yourproject\imagename.png

And for Eclipse which I am guessing you are using would be something like this

C:\Users\yourlogingname\workspace\yourproject\imagename.png

Upvotes: 0

Related Questions