Managing the opacity of a window - Java Swing

Java examples for Swing:JFrame

Description

Managing the opacity of a window

Demo Code

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Main extends JFrame {
    //from   w  w  w.j a  va 2  s .co  m
    public Main() {        
        this.setBounds(100, 100, 200, 200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setType(Type.NORMAL);
        
        JButton exitButton = new JButton("Exit");
        this.add(exitButton);
        exitButton.addActionListener(event->System.exit(0));
    }

    public static void main(String[] args) {               
        JFrame.setDefaultLookAndFeelDecorated(true);

        GraphicsEnvironment graphicsEnvironment = 
            GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice graphicsDevice = graphicsEnvironment.getDefaultScreenDevice();

        if (!graphicsDevice.isWindowTranslucencySupported(
                GraphicsDevice.WindowTranslucency.TRANSLUCENT)) {
            System.err.println(
                "Translucency is not supported on this platform");
                System.exit(0);
        }   
        
        SwingUtilities.invokeLater(()-> {
                Main window = new Main();
                window.setOpacity(0.75f);
                window.setVisible(true);
        });

    }
}

Related Tutorials