Monday 31 October 2011

Using/Adding Images in Eclipse

This is one of the challenges that most of the beginners using Eclipse face.There are a number of ways to do so but I will describe one of the easiest methods to accomplish the task.
The steps are as follows :
  1. In the Package Explorer located on the left, right-click on the src folder of your project.
  2. Click on the Import option.
  3. In the Import Dialog Box, choose General -> File System and click Next.
  4. In the From Directory option click on browse and select the folder in which your image/resource file resides.
  5. Tick the image/resource files you want to add to your project.
  6. In the Into Directory option add a folder named "resources" under src so that the into folder now reads <Project-name>/src/resources.
  7. Click the Finish button.
  8. Now you will see the desired image/resource file under <Project-name>/src/resources.
  9. Now wherever in your code you need to provide the name of the image/resource file use the following:
    For example if your image file is “picture.jpg”, use
    this.getClass().getResource(“/resources/picture.jpg”)
  10. For instance to use the “picture.jpg” as an icon we use:
    ImageIcon img = new ImageIcon(this.getClass().getResource(“/resources/picture.jpg”)) ;


Hope this helps…

No comments:

Post a Comment