2

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?

Zhao Yi
  • 2,335
  • 7
  • 23
  • 27

2 Answers2

1

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

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • Now that JDK7 delivers native support for translucent windows, why should I stick to the ugly `Robot` solution? – Zhao Yi Sep 19 '12 at 09:36
  • @ZhaoYi, i gave you a solution that i used in my project, if you think that jdk7 provides a better opportunity to get the work done, then its always better to adopt to it.... – Kumar Vivek Mitra Sep 19 '12 at 09:39
  • I have also used the `Robot` solution before, but it doesn't work when the content behind the "translucent" window is something animated (e.g. a gif or a movie)... – Zhao Yi Sep 19 '12 at 09:55
0

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?

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366