Android Dev
Android Dev

Reputation: 198

How to set transparent png to JButton?

I am making java desktop application using swing. I want to set png to jbutton. but i can't set transparent image. I want to do as in android like set background null so transparent image can be set.

Upvotes: 3

Views: 14293

Answers (5)

alain.janinm
alain.janinm

Reputation: 20065

To create a JButton with a transparent PNG, I use :

JButton jButton1 = new JButton(new ImageIcon(ImageIO.read(new File("yourImage.png")  

To create a JButton with a scaled transparent PNG, I use :

ImageIcon image = new ImageIcon("yourImage.png") 
JButton jButton1 = new JButton(new ImageIcon(getScaledImage(icon.getImage(), 32, 32)));


/**
 * Resizes an image using a Graphics2D object backed by a BufferedImage.
 * @param srcImg - source image to scale
 * @param w - desired width
 * @param h - desired height
 * @return - the new resized image
 */
private Image getScaledImage(Image srcImg, int w, int h){
    BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TRANSLUCENT);
    Graphics2D g2 = resizedImg.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2.drawImage(srcImg, 0, 0, w, h, null);
    g2.dispose();
    return resizedImg;
}

Then if you don't want visible border use :

jButton1.setOpaque(false);
jButton1.setBorderPainted(false);
jButton1.setContentAreaFilled(false);

Upvotes: 1

Mennan
Mennan

Reputation: 4497

Try this :

button.setOpaque(false);
button.setContentAreaFilled(false);
button.setBorderPainted(false);

Upvotes: 9

nIcE cOw
nIcE cOw

Reputation: 24616

Have a look at this example program, is this what you asking for ?

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

public class ButtonTransparentImage
{
    private BufferedImage originalImage, modifiedImage;
    private ImageIcon image;

    private JButton imageButton;

    private void displayGUI()
    {
        JFrame frame = new JFrame("Transparent Image on JButton");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        getModifiedImage();
        image = new ImageIcon(modifiedImage);
        imageButton = new JButton(image);
        imageButton.setBackground(Color.GREEN.darker());

        JPanel contentPane = new JPanel();
        contentPane.add(imageButton);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private void getModifiedImage()
    {
        try
        {
            originalImage = ImageIO.read(
                new URL("http://gagandeepbali.uk.to/" + 
                    "gaganisonline/images/swing/stackoverflow/geek3.gif"));
            modifiedImage = new BufferedImage(
                originalImage.getWidth(),
                originalImage.getHeight(),
                BufferedImage.TYPE_INT_ARGB);       
        }
        catch(IOException ioe)
        {
            System.out.println("Unable to read the Content of the Image.");
            ioe.printStackTrace();
        }

        Graphics2D g2 = modifiedImage.createGraphics();
        AlphaComposite newComposite = 
            AlphaComposite.getInstance(
                AlphaComposite.SRC_OVER, 0.5f);
        g2.setComposite(newComposite);      
        g2.drawImage(originalImage, 0, 0, null);
        g2.dispose();
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new ButtonTransparentImage().displayGUI();
            }
        });
    }
}

OUTPUT :

TRANSPARENT IMAGE

Simply change this line image = new ImageIcon(modifiedImage); to image = new ImageIcon(originalImage); to see the difference :-)

Upvotes: 3

Saunik Singh
Saunik Singh

Reputation: 996

ImageIcon cup = new ImageIcon("images/cup.png"); JButton button2 = new JButton(cup);

This will help you lot. for more information you can click on this link

Jbutton Tutorial

Jbutton Class

Upvotes: 2

James
James

Reputation: 2542

try button.setIcon(new ImageIcon(ImageIO.read(new File("path/to/image.png"))))

Upvotes: 3

Related Questions