Java ImageIcon Load getImageIcon(String filename, String description)

Here you can find the source of getImageIcon(String filename, String description)

Description

Keeps a hashmap of SoftReferences to images, hashed by filename, so that the same image icon will be used for multiple versions of the same image.

License

BSD License

Parameter

Parameter Description
filename Image filename (relative to IMAGE_DIR) to get
description String description of icon, or null

Declaration

public static ImageIcon getImageIcon(String filename, String description) 

Method Source Code

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

import java.util.ResourceBundle;
import javax.swing.ImageIcon;

public class Main {
    /**/*from  w  w  w  .  ja v a2s.c o m*/
       * Same as getImageIcon(String, String) with null description. Uses soft
       * references to avoid creating more than one copy of the same image, and
       * help with memory management.
       * 
       * @see #getImageIcon(String, String)
       * @param filename
       *          Image filename (relative to IMAGE_DIR) to get
       * @return ImageIcon for that filename, or null if no file exists
       */
    public static ImageIcon getImageIcon(String filename) {
        return getImageIcon(filename, null);
    }

    /**
       * Keeps a hashmap of SoftReferences to images, hashed by filename, so that
       * the same image icon will be used for multiple versions of the same image.
       * The SoftReferences ensure that if the images are no longer directly
       * reachable, they will be garbage collected before an OutOfMemoryError is
       * thrown.
       * 
       * @deprecated Use ImageBundle instead...
       * 
       * @param filename
       *          Image filename (relative to IMAGE_DIR) to get
       * @param description
       *          String description of icon, or null
       * @return
       */
    public static ImageIcon getImageIcon(String filename, String description) {

        ResourceBundle bundle = ResourceBundle.getBundle("com.stottlerhenke.simbionic.editor.gui.ImageBundle");
        try {
            ImageIcon icon = (ImageIcon) bundle.getObject(filename);

            if (icon == null)
                return new ImageIcon();
            return icon;
        } catch (Exception e) {
            return new ImageIcon();
        }
        //       File f = new File("images" + filename);
        //
        //       System.gc();
        //        if (softImages.containsKey(f)) {
        //            SoftReference sr = (SoftReference) softImages.get(f);
        //            ImageIcon icon = (ImageIcon) sr.get();
        //            if (icon != null)
        //                return icon;
        //        }
        //        ImageIcon icon;
        //        if (f.exists()) {
        //            if (description != null)
        //                icon = new ImageIcon(f.toString(), description);
        //            else
        //                icon = new ImageIcon(f.toString());
        //            softImages.put(f, new SoftReference(icon));
        //            return icon;
        //        }
        //        return null;
    }
}

Related

  1. getImageIcon(final Class baseClass, final String image)
  2. getImageIcon(final String iconName)
  3. getImageIcon(final String location)
  4. getImageIcon(Image imp, int width, int height)
  5. getImageIcon(String filename)
  6. getImageIcon(String name)
  7. getImageIcon(String name)
  8. getImageIcon(String path)
  9. getImageIcon(String path, String description)