set Button Icon - Java 2D Graphics

Java examples for 2D Graphics:Icon

Description

set Button Icon

Demo Code


//package com.java2s;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;

import java.io.File;
import java.io.IOException;

public class Main {
    public static void setButtonIcon(JButton btn, String iconPath) {
        Image icon;// ww  w  . j  a  va 2  s  .co m
        try {
            icon = ImageIO.read(new File(iconPath));
            Image scaled = icon.getScaledInstance(15, 15,
                    Image.SCALE_SMOOTH);
            btn.setIcon(new ImageIcon(scaled));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Related Tutorials