Zhao Yi
Zhao Yi

Reputation: 2465

Translucent window with Windows look and feel?

I'm playing with JDK7's translucent window support and find that it doesn't work well with Windows look and feel.

Here's my code:

JFrame.setDefaultLookAndFeelDecorated(true);
SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        JFrame frame = new JFrame("Translucent Window");
        frame.setOpacity(0.5F);
        frame.setSize(640, 360);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

This code correctly displays a translucent window. However, if I set the Windows look and feel:

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

an exception is thrown:

Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is decorated

If I disable the frame decoration with frame.setUndecorated(true), it works again but that is not what I want because the title bar is missing. Is there any way to resolve this issue?

Upvotes: 2

Views: 712

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347184

This is really stupid. Under Java 6 with AWTUtilities, this works just fine.

You'll want to read this though.

Is The Java Tutorials Translucent Window example giving trouble to those playing with jdk7?

Upvotes: 0

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

See this tutorial to understand how to make a JFrame Transparent or Translucent

http://blogofjavacrazy.blogspot.in/2007/03/transparent-window-in-java.html

I stumbled upon a neat little hack that instead of actually being transparent, it takes a screenshot and adds that little segment as the background.

See this Link:

http://onjava.com/pub/a/onjava/excerpt/swinghks_hack41/index.html

Upvotes: 1

Related Questions