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.
Asked
Active
Viewed 1.4k times
3
-
for better help sooner post an SSCCE, – mKorbel Jul 27 '12 at 12:57
5 Answers
9
Try this :
button.setOpaque(false);
button.setContentAreaFilled(false);
button.setBorderPainted(false);

David Kroukamp
- 36,155
- 13
- 81
- 138

Mennan
- 4,451
- 13
- 54
- 86
3
try button.setIcon(new ImageIcon(ImageIO.read(new File("path/to/image.png"))))

James
- 2,483
- 2
- 24
- 31
3
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 :
Simply change this line image = new ImageIcon(modifiedImage);
to image = new ImageIcon(originalImage);
to see the difference :-)

nIcE cOw
- 24,468
- 7
- 50
- 143
-
+1 for [sscce](http://sscce.org/); sadly, the UI delegate, e.g. `com.apple.laf.AquaLookAndFeel` may overrule you. – trashgod Jul 27 '12 at 17:52
-
Aha, just learning how to paint in Swing, so cann't question your judgement as always :-) . So any wonderful example for a workaround to this scenario ? – nIcE cOw Jul 27 '12 at 17:57
-
Sorry, I don't know a good, cross-platform approach; Java 7 tries to remedy that. Instead of using `setBackground()`, compose the image in `g2` using a translucent background, similar to how it's done [here](http://stackoverflow.com/a/2166500/230513) with `CLEAR`. – trashgod Jul 27 '12 at 18:04
2
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

Saunik Singh
- 996
- 1
- 9
- 19
1
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);

alain.janinm
- 19,951
- 10
- 65
- 112
-
If you do not want the focus border to be displayed, also add jButton1.setFocusPainted(false) – Jean-Marc Astesana Nov 04 '22 at 16:27