I would like to know how to get a transparent JFrame
in the latest version of Java.
Currently, you can only use
<JFrame>.setOpacity();
if the frame is not decorated.
I have no use for an undecorated frame, so I'd like to know how to go around this restriction and set the opacity of the frame to 0.5f
while still keeping the title bar, resize options etc.
I have read the docs here: http://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html. The code only worked on Java 6 and no longer runs. The error, as I said, is:
Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is decorated
at java.awt.Frame.setOpacity(Frame.java:960)
at TranslucentWindowDemo$1.run(TranslucentWindowDemo.java:53)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
...
I have also tried setting the background (setBackground : Color)
using a Color
with custom Alpha value (new Color(int, int, int, Alpha)
) but it throws the exact same error.
Setting the transaprency of a JPanel
this way won't work, as it will still lay on the JFrame
, which is not transparent.
I could find no other answer on Stack Overflow that correctly addressed this issue. In fact, a few suggested that this could be fixed with:
JFrame.setDefaultLookAndFeelDecorated(true);
But they were misinformed of perhaps referring to Java 7, as I have tested it and the result is just the same.
I have also tried to manually set the Look And Feel:
try {
for (final LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch [...]
And combining this with the solution suggested above also did not work.
Please refer to the code over to the example I linked above (Oracle doc) for a MCVE, as that is the one I'm using.
Any way around this?
setUndecorated(true)
and likelysetDefaultLookAndFeelDecorated(false);
– Collative0.5f
opacity or should it stay fully visible? – DarellsetDefaultLookAndFeelDecorated(true);
. The window is still decorated. – DarellsetOpacity
was introduced in Java 7. Java 6 did not support transparency before that (at least with this API). The code in the Oracle tutorial works for me with Java 8 (on Ubuntu). – TarsometatarsusJFrame.setDefaultLookAndFeelDecorated(true);
But they were misinformed of perhaps referring to Java 7, as I have tested it and the result is just the same." – DarellUIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
. Maybe he has set it in hisswing.properties
, which then prevents to run the examples? – Tarsometatarsus