0

When I try to run to get transparent frame it shows exception.

My code is:

public class NewJFrame extends javax.swing.JFrame {
    public NewJFrame() {
        initComponents();
        com.sun.awt.AWTUtilities.setWindowOpacity(this, 0.05f);
    }

Exception is:

Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is decorated
at java.awt.Frame.setOpacity(Frame.java:960)
at java.awt.Window$1.setOpacity(Window.java:4032)
at com.sun.awt.AWTUtilities.setWindowOpacity(AWTUtilities.java:174)
at test.NewJFrame.<init>(NewJFrame.java:28)
at test.NewJFrame$2.run(NewJFrame.java:115)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Dipen Ahir
  • 19
  • 2
  • 11

2 Answers2

2

Call setUndecorated(true) before you call setWindowOpacity.

In Java 7+ this support is provided in the core API (without the need to use a com.sun.* library). See How to Create Translucent and Shaped Windows for more details. For example, you could use JFrame#setOpacity instead...

this.setOpacity(0.05f);

Oh, and despite what the tutorial might suggest, I believe the only way to make a decorated window transparent is when the window is NOT using the OS provided decorations (and is using the look and feel provided decorations, which not all look and feels support), but I could be wrong

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • i m using netbeans 8.1 , and i also try to setUndecorated(true) but error is same. – Dipen Ahir Feb 23 '15 at 10:33
  • Netbeans as nothing to do with, it has to do with the version of Java you're using. Is the window visible when you try and un-decorate it or change it's opacity – MadProgrammer Feb 23 '15 at 10:38
  • jdk-8u5-windows-i586, code is try { this.setUndecorated(true); this.setOpacity(0.5f); } catch (Exception e) { JOptionPane.showMessageDialog(null, e); //System.out.println(e); } Exception is :java.awt.IllegalComponentStateException: The frame is displayable. – Dipen Ahir Feb 23 '15 at 11:00
  • You've tried to modify the state after it's all attached to a native peer, either it's visible or you've set it's size in some way... – MadProgrammer Feb 23 '15 at 11:12
  • java.awt.IllegalComponentStateException: The frame is displayable. – Dipen Ahir Feb 23 '15 at 11:24
  • Is there any way to do that without losing decoration? – Cornelius Jan 17 '17 at 09:21
  • @Cornelius At the time of writing, none that I was aware of, it was two years ago and I've not tried lately – MadProgrammer Jan 17 '17 at 19:33
  • @MadProgrammer Thanks for answering anyway. I ended up adding my own icons in upper right corner. – Cornelius Jan 17 '17 at 19:49
  • For future readers of this post, take a look at [this answer](https://stackoverflow.com/a/56961097/6579265) where you can do it while using `System Look & Feel`. – George Z. Jul 09 '19 at 22:12
  • @GeorgeZ. A be prepared for it to blow up in your face ... just saying ;) – MadProgrammer Jul 09 '19 at 22:56
  • @MadProgrammer I did not understand what you mean :( – George Z. Jul 09 '19 at 22:58
  • 1
    @GeorgeZ. Any solution which relies on reflection is a "hack" at best and is prone to either breaking in future releases or breaking on different platforms. I've not tested this on Mac or Linux, both of which are more sensitive about these types of things. Using reflection this way breaking the intended functionality of the object and could cause untold issues - yes I've done it, yes it's blown up in my face, just saying ;) – MadProgrammer Jul 09 '19 at 23:04
0

As an alternative; JavaFX supports transparent windows natively (See for example this tutorial). If you are just starting with Java GUI programming and have no compelling reason for the use of Swing (ie. a legacy app) I recommend switching to JavaFX. Swing is in maintenance mode and all the new stuff is going into JavaFX. I doubt Swing will ever receive a single new feature from Oracle.

Jasper Siepkes
  • 1,412
  • 16
  • 25