Java Swing Icon getIcon(String f, Class c)

Here you can find the source of getIcon(String f, Class c)

Description

Get an Icon from the JAR file.

License

GNU General Public License

Parameter

Parameter Description
f The name of the icon file.
c The class where it is to be found.

Return

The Icon, or null.

Declaration

public static Icon getIcon(String f, Class<?> c) 

Method Source Code

//package com.java2s;
/*//from w ww.ja  v  a2  s.  c  o  m
 * Copyright (C) 2015  University of Oregon
 *
 * You may distribute under the terms of either the GNU General Public
 * License or the Apache License, as specified in the LICENSE file.
 *
 * For more information, see the LICENSE file.
 */

import java.io.*;
import java.net.*;

import javax.swing.Icon;
import javax.swing.ImageIcon;

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

public class Main {
    /**
     * Get an Icon from the JAR file.
     * @param f The name of the icon file.
     * @param c The class where it is to be found.
     * @return The Icon, or null.
     */
    public static Icon getIcon(String f, Class<?> c) {
        Icon imageIcon = null;
        try {
            URL imageURL = c.getResource(f);
            if (imageURL == null) {
                return null;
            }
            java.awt.image.ImageProducer I_P;
            I_P = (java.awt.image.ImageProducer) imageURL.getContent();
            Toolkit tk = Toolkit.getDefaultToolkit();
            Image img = tk.createImage(I_P);
            if (img == null) {
                return null;
            }
            imageIcon = new ImageIcon(img);
        } catch (IOException e) {
            return null;
        }
        return imageIcon;
    }
}

Related

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