Java ImageIcon Load getImageIcon(final String iconName)

Here you can find the source of getImageIcon(final String iconName)

Description

get Image Icon

License

Open Source License

Declaration

public static ImageIcon getImageIcon(final String iconName) 

Method Source Code


//package com.java2s;

import java.awt.Image;
import java.awt.Toolkit;

import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class Main {
    public static ImageIcon getImageIcon(final String iconName) {
        return new ImageIcon(getImage(iconName));
    }//from w  ww  .j  a v  a 2 s .  c  o  m

    public static ImageIcon getImageIcon(final String iconName, final ClassLoader classLoader) {
        final Image image = getImage(iconName, classLoader);
        if (image == null) {
            return null;
        }
        return new ImageIcon(image);
    }

    public static Image getImage(final String imageName, final ClassLoader classLoader) {
        final URL url = classLoader.getResource("bitmaps/" + imageName);
        if (url == null) {
            return null;
        }

        try {
            return ImageIO.read(url);
        } catch (final IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static Image getImage(final String imageName) {
        return Toolkit.getDefaultToolkit().getImage("bitmaps/" + imageName);
    }
}

Related

  1. getImageIcon(Class _class, String resourceName)
  2. getImageIcon(final Class baseClass, final String image)
  3. getImageIcon(final String location)
  4. getImageIcon(Image imp, int width, int height)
  5. getImageIcon(String filename)
  6. getImageIcon(String filename, String description)