3
lblImage = new javax.swing.JLabel();
lblImage.setIcon(new javax.swing.ImageIcon("E:..path...png"));

I added the file like this. I know it is not accessing the path when running the jar file. Help me how to import a image file to java project in Netbeans.

Barett
  • 5,826
  • 6
  • 51
  • 55
  • You should not do like this. Keep all the images in a resource folder in your project itself and get it by getResource method. – vels4j Dec 31 '12 at 07:08
  • @vels4j: There is no default resource folder. Do I have to manually make a folder and name it as resource or is there any other way in net beans to create a default resource folder? – user1914867 Dec 31 '12 at 07:22
  • you can create your own /projectname/resource/image.png – vels4j Dec 31 '12 at 07:30
  • Please have a look at this answer regarding how to [**add images to your NET BEANS project**](http://stackoverflow.com/a/9583367/1057230), though after following the steps you have to access your images like this `jLabel.setIcon(new ImageIcon(getClass().getResource("/resources/images/image.extension")));` – nIcE cOw Dec 31 '12 at 07:35
  • if i want to load another file say xml file then what should i do ?(ex->new file(/path/sample.xml)); – – user1914867 Dec 31 '12 at 07:35
  • Make separate folders for each category of related files inside your `resources` Folder and access them as required. – nIcE cOw Dec 31 '12 at 07:37
  • @gagandeep:File f=new File(path/sample.xml);This xml file not accesing when i run the jar file after clean and built the projct.please help – user1914867 Dec 31 '12 at 07:44
  • @user1914867 : Please find attached [**NetBeans Project**](http://gagandeepbali.uk.to/gaganisonline/swing/downloads/SwingSample.zip), that might can help you a bit more. – nIcE cOw Dec 31 '12 at 08:29
  • @Gandeep:But in the the above project still when i am cleaning and building the dist folder is created.when i tried to run jar file from the dist folder it is not working – user1914867 Dec 31 '12 at 08:54
  • @user1914867 : Please do try the link again, now I had added a working thingy in that, and do have a look for, how to access files inside the code. **Line Numbers 30-31**. In order to access files from **JAR FILE**, you have to use [**YourClassName.class.getClass().getResourceAsStream("/resources/files/yourFile.extension")**](http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)) – nIcE cOw Dec 31 '12 at 11:45

2 Answers2

6

Recommended way is place images inside resource folders and use it as below:

jLabel1.setIcon(new ImageIcon(getClass().getResource("/path/to/image.png"))); 

Read More: NetBeans Doc

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Chandana
  • 2,578
  • 8
  • 38
  • 55
  • There is no default resource folder. Do I have to manually make a folder and name it as resource or is there any other way in net beans to create a default resource folder? – user1914867 Dec 31 '12 at 07:21
  • The way NetBeans is suggested is typically you create a separate Java package for the resources and place your image resources under that. – Chandana Dec 31 '12 at 07:25
  • if i want to load another file say xml file then what should i do ?(ex->new file(/path/sample.xml)); – user1914867 Dec 31 '12 at 07:34
  • if you have multiple type of resources, then create separate packages like `image`, `xml` – Chandana Dec 31 '12 at 07:43
  • :please show me an example like this(File f=new File(path/sample.xml))so that i can access it from the package. – user1914867 Dec 31 '12 at 07:48
1

Create the image folder in the src folder, put the images in the folder and give the relative path for creating the image icon object

     jLabel1.setIcon(new ImageIcon(getClass().getResource("/image/img1.jpg")));

then rebuild the jar .it will access the images from the jar

Adesh singh
  • 2,083
  • 9
  • 24
  • 36