Java Swing Icon getIcon(Class baseclass, String name)

Here you can find the source of getIcon(Class baseclass, String name)

Description

E.g.

License

Open Source License

Parameter

Parameter Description
baseclass determines the package where to seek for icon.
name of the icon

Declaration

public static final ImageIcon getIcon(Class baseclass, String name) 

Method Source Code

//package com.java2s;
/*//from w w  w. j ava 2 s  . c o m
 * Copyright 2001-2008 Aqris Software AS. All rights reserved.
 * 
 * This program is dual-licensed under both the Common Development
 * and Distribution License ("CDDL") and the GNU General Public
 * License ("GPL"). You may elect to use one or the other of these
 * licenses.
 */

import javax.swing.ImageIcon;

import java.net.URL;

public class Main {
    /**
     * E.g. if baseclass is <code>a.b.c</code>, then it will seek for:<br>
     * /a/b/images/&lt;name&gt
     *
     * @param baseclass determines the package where to seek for icon.
     * @param name of the icon
     *
     * @return {@link ImageIcon}
     */
    public static final ImageIcon getIcon(Class baseclass, String name) {
        try {
            return new ImageIcon(baseclass.getResource("images/" + name));
        } catch (NullPointerException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * E.g. if baseclass is <code>a.b.c</code>, then it will seek for:<br>
     * /a/b/resources/&lt;name&gt;<br>
     * and return an {@link URL}
     *
     * @param baseclass determines the package where to seek for resource.
     * @param name of the resource.
     *
     * @return {@link URL}
     */
    public static final URL getResource(Class baseclass, String name) {
        return baseclass.getResource("resources/" + name);
    }
}

Related

  1. getDefaultIcon()
  2. getEmptyIcon()
  3. getFolderIcon()
  4. getFolderIcon()
  5. getIcon(boolean isPressed, Icon baseIcon_)
  6. getIcon(Class clazz, String path)
  7. getIcon(final String path)
  8. getIcon(final String resource)
  9. getIcon(int index)