Java ImageIcon Load getImageIcon(String path, String description)

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

Description

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

License

Open Source License

Parameter

Parameter Description
path can't be <code>null</code> or whitespace, use forward slashes even on Windows systems
description a parameter

Return

see above

Declaration

static ImageIcon getImageIcon(String path, String description) 

Method Source Code

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

import java.net.URL;

import javax.swing.ImageIcon;

public class Main {
    /**/*from w  w  w.j  av  a 2s.c o m*/
     * Returns an ImageIcon, or null if the path was invalid.
     * 
     * @param path can't be <code>null</code> or whitespace, use
     *    forward slashes even on Windows systems
     * @param description
     * @return see above
     */
    static ImageIcon getImageIcon(String path, String description) {
        // Check path is valid
        if (path == null || path.trim().length() == 0) {
            throw new IllegalArgumentException("Invalid file path: " + path);
        }

        URL imgURL = ClassLoader.getSystemResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL, description);
        }

        throw new IllegalArgumentException("Couldn't find file: " + path);
    }
}

Related

  1. getImageIcon(String filename)
  2. getImageIcon(String filename, String description)
  3. getImageIcon(String name)
  4. getImageIcon(String name)
  5. getImageIcon(String path)
  6. getImageIcon(String res)
  7. getImageIcon(String resource)
  8. getImageIcon(URL u, int w)
  9. getImageIcon(URL url)