title: Want a JLabel with an image that has a transparent background
body: <p>I use this icon for a JLabel in a JTable's custom renderer.  When the row in the table is selected, the icon background shows up white.</p>
  
  <p>I've used paint.net to create a green triangle, and set its background to white with an alpha of 255.  That's the image I use in this code to create an IconImage for the JLabel; I use varying widths for the icon for external reasons.  Here's an example program showing what is done:</p>
  
  <pre><code>package spacecheck.images;
  
  import java.awt.Color;
  import java.awt.Graphics;
  import java.awt.image.BufferedImage;
  import java.util.ArrayList;
  
  import javax.swing.ImageIcon;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  
  /**
   * represents an icon used in the directory tree; handles 'expanded' and
   * 'unexpanded' directories as well as indentation representing different
   * levels.
   * @author rcook
   *
   */
  public class TreeIconExample
  {
    public static int UNEXPANDED = 1;
    public static int EXPANDED = 2;
  
    @SuppressWarnings({"unused"})
    private void say (String msg) { System.out.println(msg); }
  
    private static ImageIcon expandedIcon = null;
    private static ImageIcon unexpandedIcon = null;
    private static int       iconHeight = 0;
    private static int       iconWidth  = 0;
  
    private static ArrayList&lt;ImageIcon&gt; cachedExpandedIcons = new ArrayList&lt;ImageIcon&gt;();
    private static ArrayList&lt;ImageIcon&gt; cachedUnexpandedIcons = new ArrayList&lt;ImageIcon&gt;();
  
    static
    {
      expandedIcon = new ImageIcon(TreeIconExample.class.getResource("images/Expanded.GIF"));
      unexpandedIcon = new ImageIcon(TreeIconExample.class.getResource("images/Unexpanded.GIF"));
      iconHeight = unexpandedIcon.getIconHeight();
      iconWidth =  unexpandedIcon.getIconWidth();
    }
  
    public TreeIconExample()  {  }
  
    public static void main(String ... arguments)
    {
      JFrame frame = new JFrame("icon test");
      frame.setBackground(Color.blue);
      JLabel label = new JLabel("background test");
      label.setBackground(Color.magenta);
      TreeIconExample treeIcon = new TreeIconExample();
      ImageIcon icon = treeIcon.getIcon(2, false);
      label.setIcon(icon);
      frame.add(label);
      frame.pack();
      frame.setVisible(true);
    }
  
    /**
     * return the icon for an expanded or unexpanded level
     * @param int level of folder relative to other levels displayed;
     * starts at 0 and increases with depth
     * @param boolean indicates whether this level is expanded or not.
     * @return ImageIcon appropriate for expansion flag and level.
     */
    public ImageIcon getIcon(int level, boolean expanded)
    {
      ImageIcon result = null;
  
      // generate this icon and store it in the cache before returning it.
      ImageIcon baseIcon = unexpandedIcon;
      if (expanded) { baseIcon = expandedIcon; }
      int iconH = iconHeight;
      int iconW = iconWidth*(level+1);
  
      BufferedImage bufferedImage = new BufferedImage(iconW,iconH,BufferedImage.TYPE_INT_ARGB);
      Graphics g = bufferedImage.getGraphics();
  
      g.fillRect(0, 0, iconW, iconH);
      g.drawImage(baseIcon.getImage(), iconWidth*level, 0, null);
      result = new ImageIcon(bufferedImage);
  
      return result;
    }
  }
  </code></pre>
  
  <p>And here is my result:</p>
  
  <p><img src="https://i.stack.imgur.com/tPuBy.png" alt="enter image description here"></p>
  
  <p>what I would like to do is eliminate the white portion of the icon; I'd like that to be transparent, so the background of the JLabel shows through.  I don't know why neither magenta nor blue show up in this program; if someone cares to tell me that, I'd appreciate it.  But the transparent background on the image is the main thing I'm trying to figure out.</p>