Java JTree getTreeImage(String imageName)

Here you can find the source of getTreeImage(String imageName)

Description

get Tree Image

License

Open Source License

Parameter

Parameter Description
imageName the String representing the image to be returned

Return

an ImageIcon from anywhere in the classpath

Declaration

public static ImageIcon getTreeImage(String imageName) 

Method Source Code

//package com.java2s;
// The JavaPOS Config/Loader (aka JCL) is now under the CPL license, which 

import java.util.Vector;
import java.io.*;
import java.awt.Toolkit;
import javax.swing.*;

public class Main {
    public static final String PATHFORIMAGEFILES = "jpos/res/images/";

    /** /*from ww w  . j  a v  a 2 s  .c o  m*/
     * @return an ImageIcon from anywhere in the classpath
     * @param imageName the String representing the image to be returned
     * @since 1.3 (SF 2K meeting)
     */
    public static ImageIcon getTreeImage(String imageName) {
        String imageFileName = PATHFORIMAGEFILES + imageName;

        ImageIcon treeImageIcon = null;

        try {
            Vector v = new Vector();

            InputStream is = ClassLoader.getSystemClassLoader()
                    .getResourceAsStream(imageFileName);

            if (is == null)
                is = ClassLoader
                        .getSystemResourceAsStream(PATHFORIMAGEFILES
                                + "unknown.gif");

            is = new BufferedInputStream(is);

            while (is.available() > 0) {
                byte[] buffer = new byte[is.available()];

                is.read(buffer);

                v.addElement(buffer);
            }

            int size = 0;

            for (int i = 0; i < v.size(); ++i) {
                byte[] buffer = (byte[]) v.elementAt(i);

                size += buffer.length;

            }

            byte[] bigBuffer = new byte[size];
            int dstPos = 0;

            for (int i = 0; i < v.size(); ++i) {
                byte[] buffer = (byte[]) v.elementAt(i);

                System.arraycopy(buffer, 0, bigBuffer, dstPos,
                        buffer.length);

                dstPos += buffer.length;
            }

            treeImageIcon = new ImageIcon(Toolkit.getDefaultToolkit()
                    .createImage(bigBuffer));

        } catch (IOException ioe) {
            treeImageIcon = null;
        }

        return treeImageIcon;
    }
}

Related

  1. getCurrentWidth(JTree tree, int row)
  2. getExpansionState(JTree tree, int row)
  3. getProjectTree()
  4. getSelectionFromTree(JTree tree)
  5. getStopRow(JTree tree, int startRow)
  6. getTreeObject(JTree tree, Class type)
  7. getTreeSelectionBorderColor()
  8. isPointOnTree(JTree tree, Point location)
  9. maxDepth(JTree tree, int depth)