Java Swing Icon getResourceIcon(Object target, String name)

Here you can find the source of getResourceIcon(Object target, String name)

Description

{ method

License

Apache License

Parameter

Parameter Description
target - Object holding resource
name the file name

Return

Icon for the resource possibly null }

Declaration

public static Icon getResourceIcon(Object target, String name) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.awt.*;
import java.io.*;

import javax.swing.*;

public class Main {
    /**/*from   w w  w. j a  v  a  2 s.co m*/
     * { method
     *
     * @param target -  Object holding resource
     * @param name   the file name
     * @return Icon for the resource possibly null
     *         }
     * @name getResourceIcon
     * @function read an Icon as a Resource
     */
    public static Icon getResourceIcon(Object target, String name) {
        return (getResourceIcon(target.getClass(), name));
    }

    /**
     * { method
     *
     * @param target -  Class holding resource
     * @param name   the file name
     * @return Icon for the resource possibly null
     *         }
     * @name getResourceIcon
     * @function read an Icon as a Resource
     */
    public static Icon getResourceIcon(Class target, String name) {
        try {
            // Netscape appears not to support getResource so this
            // reads an image from a resource
            //

            InputStream is = target.getResourceAsStream(name);
            if (is == null) {
                System.err.println("Resource not found. " + name + " as Reference in class " + target.getName());
                return (null);
            }
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int c;
            while ((c = is.read()) >= 0)
                baos.write(c);
            is.close();
            Image TheActualImage = Toolkit.getDefaultToolkit().createImage(baos.toByteArray());
            Icon TheImage = new ImageIcon(TheActualImage);
            return (TheImage);
        } catch (IOException e) {
            return (null);
        } catch (Exception ex) {
            //  return(SecurityUtilities.secureGetResourceIcon(target,name) );
            return (null);
        }
    }
}

Related

  1. getIconCheckBox(String iconPath, String pressIconPath, String rolloverIconPath, String selectedIconPath)
  2. getIconFromExtension(String f, boolean folder)
  3. getIconInterTrial()
  4. getIcono()
  5. getPressedExitIcon(int x, int y)
  6. getScaledIcon(Icon icon)
  7. getScaledIcon(Icon tmpIcon, int w, int h, boolean incr)
  8. getScaledIcon(String resourceName, int width, int height)
  9. getSize(String graphIconPath)