Saturday, October 19, 2013

Transparent Window

Sometimes, you might want to make your window transparent so that any thing behind the window can be seen. For older Java versions under Java 7, you can not simply make the window transparent. In Java 7, it works. In the example code below, the window created by the JFrame component is transparent.

jframe window transparent

There are two methods of the JFrame class that you will use to make the window transparent. The first method is setUndecorated. Before you can change the transparency of the window, it is required the window undecorated (e.g without border, title). By providing the true value to the setUndecorated method, the window is now undecorated. You should note that the window can be undecorated when it is not displayable. So, you must call the the setVisible method to show the window after calling the setUndecorated method. Otherwise, an error will occur.
The second method is setOpacity. This method accepts the opacity value between 0.0 and 1.0. To make the window completely transparent, you will supply 0.0 to this method. In default, this value is 1.0 representing the complete opaque window (not transparent).

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

public class WindowTran {
public static void main(String[] args){
JFrame window=new JFrame();
window.setSize(new Dimension(400,300));
Container cont=window.getContentPane();
JButton bt=new JButton(new Action("", new ImageIcon("d:/gmaptr.png")));


static class Action extends AbstractAction{
Action(String title){
Action(String title, Icon image){
super(title, image);
public void actionPerformed(ActionEvent e){


  1. nice article in your blog.thank you for sharing useful info.
    web programming tutorial

  2. It’s actually a nice and helpful piece of information. I’m glad that you shared this useful info with us. Please keep us informed like this. Thanks for sharing. low cost Air ambulance services

  3. Information from this blog is very useful for me, am very happy to read this blog Kindly visit us @ Luxury Watch Box | Shoe Box Manufacturer |  Candle Packaging Boxes