Java ImageIcon Create createImageIcon(String path, String description)

Here you can find the source of createImageIcon(String path, String description)

Description

Returns an ImageIcon, or null if the path was invalid.

License

Open Source License

Parameter

Parameter Description
path Relative path to image (from ClassLoader's original class location) (e.g. "images/image1.png")
description Image description

Return

Icon based on image file in path

Declaration

public static ImageIcon createImageIcon(String path, String description) 

Method Source Code


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

import javax.swing.*;

public class Main {
    /**//from w w  w.j  av a2  s  .co  m
     * Returns an ImageIcon, or null if the path was invalid.
     *
     * @param path        Relative path to image (from ClassLoader's original class location) (e.g. "images/image1.png")
     * @param description Image description
     * @return Icon based on image file in path
     */
    public static ImageIcon createImageIcon(String path, String description) {
        java.net.URL imgURL = Thread.currentThread().getContextClassLoader().getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL, description);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
}

Related

  1. createImageIcon(String path)
  2. createImageIcon(String path, Class resourceClass)
  3. createImageIcon(String path, Class c)
  4. createImageIcon(String path, int width, int height)
  5. createImageIcon(String path, String description)
  6. createImageIcon(String resourcePath)
  7. createImageIcon(URL url)
  8. createImageIcon(URL url, String description, Integer w, Integer h)
  9. createImageIconJLabel(ClassLoader classloader, String path, String description, String text)