Java Swing Icon getIcon(String _package, String iconName)

Here you can find the source of getIcon(String _package, String iconName)

Description

Gets the icon in the specified package with the specified name.

License

Open Source License

Parameter

Parameter Description
_package a parameter
iconName a parameter

Return

The icon, or null if the icon doesn't exist

Declaration

public static ImageIcon getIcon(String _package, String iconName) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.net.URL;

import javax.swing.ImageIcon;

public class Main {
    /**//from ww w.j a  v a 2  s .co  m
     * Gets the icon in the specified package with the specified name.
     *
     * A package might be UIUtils.X16, UIUtils.X32, etc
     *
     * @param _package
     * @param iconName
     * @return The icon, or null if the icon doesn't exist
     */
    public static ImageIcon getIcon(String _package, String iconName) {
        if (!_package.endsWith("/")) {
            _package += "/";
        }
        return getIcon(_package + iconName);
    }

    /**
     * Gets the icon at the specified path
     *
     * @param path The path of the icon
     * @return The icon, or null if the icon file doesn't exist
     */
    public static ImageIcon getIcon(String path) {
        return createImageIcon(path);
    }

    /**
     * Create an ImageIcon from the image at the specified path
     *
     * @param path The path of the image
     * @return The image icon, or null if the image doesn't exist
     */
    public static ImageIcon createImageIcon(String path) {

        URL u = Thread.currentThread().getContextClassLoader().getResource(path);
        //URL u = ClassLoader.getSystemResource(path);//UIUtils.class.getResource(path);
        if (u == null) {
            return null;
        }
        return new ImageIcon(u);
    }
}

Related

  1. getIcon(Class baseclass, String name)
  2. getIcon(Class clazz, String path)
  3. getIcon(final String path)
  4. getIcon(final String resource)
  5. getIcon(int index)
  6. getIcon(String f, Class c)
  7. getIcon(String name)
  8. getIcon(String name)
  9. getIcon(String name)