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

    1. Great Article android based projects

      Java Training in Chennai Project Center in Chennai Java Training in Chennai projects for cse The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

  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

  4. Nice blog information thanks for sharing .I am really impressed with your writing abilities

    โปรโมชั่นGclub ของทางทีมงานตอนนี้แจกฟรีโบนัส 50%
    เพียงแค่คุณสมัคร สล็อตออนไลน์ กับทางทีมงานของเราเพียงเท่านั้น
    สมัครสล็อตออนไลน์ >>> Goldenslot
    สนใจร่วมสนุกกับ คาสิโนออนไลน์ คลิ๊กได้เลย
    มีทั้งคาสิโนออนไลน์ หวยออนไลน์ ฟุตบอลออนไลน์ สล็อตออนไลน์ และอื่นๆอีกมากมาย