Java ImageIcon Load loadImageIcon(Class clas, String iconPath)

Here you can find the source of loadImageIcon(Class clas, String iconPath)

Description

Loads a icon from either the file system or from a jar.

License

Open Source License

Parameter

Parameter Description
clas A class used to resolve the icon's relative path name
iconPath the path relative to the clas in which to find the icon.

Return

an image icon for the icon, or null if not found
 for a directory structure like edu/ mypackage/ MyClass.class images/ myicon.gif call loadImageIcon(MyClass.class, "images/myicon.gif"); 

Declaration

public static ImageIcon loadImageIcon(Class clas, String iconPath) 

Method Source Code

//package com.java2s;

import java.net.URL;

import javax.swing.ImageIcon;

public class Main {
    /**//w ww . j  a v  a2  s . c o m
     * Loads a icon from either the file system or from a jar.
     *
     * @param clas A class used to resolve the icon's relative path name
     * @param iconPath the path relative to the clas in which to find the icon.
     * @return an image icon for the icon, or null if not found
     *
     * <pre>
     * for a directory structure like
     *  edu/
     *       mypackage/
     *          MyClass.class
     *          images/
     *             myicon.gif
     *
     * call loadImageIcon(MyClass.class, "images/myicon.gif");
     *
     * </pre>
     */
    public static ImageIcon loadImageIcon(Class clas, String iconPath) {
        ImageIcon icon = null;
        URL url = clas.getResource(iconPath);
        if (url != null) {
            icon = new ImageIcon(url);
        }
        return icon;
    }
}

Related

  1. getImageIcon(String resource)
  2. getImageIcon(URL u, int w)
  3. getImageIcon(URL url)
  4. getScaledInstance(Image img, int targetWidth, int targetHeight)
  5. loadImageIcon(byte bytes[], String descr)
  6. loadImageIcon(String imageName)
  7. loadImageIcon(String url)
  8. loadResourceImageIcon(String imageName)
  9. readImageIconFromFile(File srcFile)