get Image Icon from File - Java Swing

Java examples for Swing:Icon

Description

get Image Icon from File

Demo Code


import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

public class Main{
    public static void main(String[] argv) throws Exception{
        String fileName = "java2s.com";
        System.out.println(getImageIcon(fileName));
    }/* w w w .  jav  a2  s. c om*/
    public static ImageIcon getImageIcon(String fileName) {

        if (ThreadPoolManager.isDebugMode) {
            BufferedImage bufferedImage = null;
            try {
                String pathPrefix = "resources\\images\\";//todo change it to your pc address
                bufferedImage = ImageIO
                        .read(new File(pathPrefix + fileName));
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("desire image not fund : " + fileName);
            }
            ImageIcon imageIcon = new ImageIcon(bufferedImage);
            return imageIcon;

        } else {
            //at jar file
            return new ImageIcon(ComponentUtil.class.getClassLoader()
                    .getResource(fileName));
        }
    }
}

Related Tutorials