Set opacity of a decorated JFrame in Java 8

As far as I can tell, the basic answer is: it is not possible, at least with the System look and feel. As indicated in Is The Java Tutorials Translucent Window example giving trouble to those playing with jdk7?, the JavaDocs clearly indicate that “the window must be undecorated” for setOpacity() to work.

It is however possible to do it with the (ugly) Cross-platform look and feel, that you can progrmmatically set as follows:

UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());

In fact, as the cross-platform look and feel could be overridden through configuration, the safest would actually be to set it explicitly to Metal as follows:

UIManager.setLookAndFeel(new MetalLookAndFeel());

The reason this works, is that the JDK implementation of Frame.setOpacity() throws an exception when !isUndecorated(), and JFrame.frameInit() sets itself as undecorated when the look and feel's getSupportsWindowDecorations() returns true. It then calls getRootPane().setWindowDecorationStyle() with JRootPane.FRAME, indicating that the decorations will be provided by the root pane instead of the frame.

From what I can see in the JDK, the Metal look and feel is the only one for which getSupportsWindowDecorations() returns true, as it is the only one which overrides it, and the default implementation simply returns false.

However, some third-party look and feels support it too. This is the case for instance for the Tiny Look and Feel, as I just tried:

TranslucentWindowDemo with TinyLAF

(Note that I took this screenshot on Ubuntu, TinyLAF just so happens to have a default theme that looks like Windows XP!)

See also this question for a list of known third-party look and feels.


Try adding this line before creating the JFrame window:

JFrame.setDefaultLookAndFeelDecorated(true);

Exactly that line, don't replace JFrame in the beggining, it needs to be JFrame.

(You can also spot this line in the tutorials you mentioned precicely placed before creating the window).