Java JFrame Icon setIconImageFromResource(JFrame frame, String resource)

Here you can find the source of setIconImageFromResource(JFrame frame, String resource)

Description

Set the icon image for a JFrame from a resource in its class.

License

Open Source License

Parameter

Parameter Description
frame The JFrame.
resource Path of the resource, i.e. "images/main.gif", relative to the class.

Declaration

public static void setIconImageFromResource(JFrame frame, String resource) 

Method Source Code

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

import java.awt.Image;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

public class Main {
    /**// w w w .  j  a  v a2  s  .co m
     * Set the icon image for a JFrame from a resource in its class. If the
     * image is null, it will set that, which should result in using the default
     * image.
     * 
     * @param frame The JFrame.
     * @param resource Path of the resource, i.e. "images/main.gif", relative to
     *            the class.
     */
    public static void setIconImageFromResource(JFrame frame, String resource) {
        Image image = null;
        if (frame != null && resource != null) {
            image = getImageFromClassResource(frame.getClass(), resource);
        }
        frame.setIconImage(image);
    }

    /**
     * Get an Image from a resource in a class.
     * 
     * @param cls The class.
     * @param resource Path of the resource, i.e. "images/main.gif", relative to
     *            the class.
     * @return
     */
    public static Image getImageFromClassResource(Class<?> cls, String resource) {
        if (cls == null || resource == null)
            return null;
        java.net.URL imgURL = cls.getResource(resource);
        if (imgURL != null) {
            return new ImageIcon(imgURL).getImage();
        } else {
            return null;
        }
    }
}

Related

  1. SET_FRAME_ICON(JFrame f, String iconPath)
  2. setApplicationIcon(JFrame frame)
  3. setDefaultIcon(final JFrame window, final String imagepath)
  4. setIcon(javax.swing.JFrame jFrame, String icon)
  5. setIconImage(JFrame frame, Class clazz, String path)
  6. setIconOnFrame(JFrame frame)
  7. setWindowIcon(JFrame jd)