Is the Java tutorial example a translucent window for users playing with jdk7?

Here is an example .

If you create and run TranslucentWindow , say NetBeans IDE 7.0, which supports jdk7, you will get the following exception:

 Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is decorated at java.awt.Frame.setOpacity(Frame.java:960) at main.TranslucentWindow.<init>(TranslucentWindow.java:23) at main.TranslucentWindow$1.run(TranslucentWindow.java:47) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705) at java.awt.EventQueue.access$000(EventQueue.java:101) at java.awt.EventQueue$3.run(EventQueue.java:666) at java.awt.EventQueue$3.run(EventQueue.java:664) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:675) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105) at java.awt.EventDispatchThread.run(EventDispatchThread.java:90) BUILD SUCCESSFUL (total time: 1 second) 

According to this stack trace, the violation line is tw.setOpacity(0.55f) . But, as the error indicates, if you call setUndecorated(true) in the frame, then it will not throw an exception and will create a translucent window, although without any decoration (which is a pain). This is normal? Should this code work correctly out of the box? Am I missing something?

EDIT

Why does their translucent window look decorated or is it a custom rendering?

What it is...

enter image description here

What should it be ...

enter image description here

+7
java java-7 swing translucency
source share
5 answers

Straight from JavaDocs for java.awt.frame.setOpacity () in JDK7 :

To set the opacity to less than 1.0f, the following conditions must be met:

  • Translucency TRANSLUCENT must be supported by the underlying system.
  • The window should not be decorated (see setUndecorated (boolean) and Dialog.setUndecorated (boolean))
  • The window should not be in full screen mode (see GraphicsDevice.setFullScreenWindow (Window))

If the requested opacity value is less than 1.0f and any of the above conditions are not met, the opacity of the window will not change and an IllegalComponentStateException will be thrown.

The behavior you see is documented and expected behavior.

+14
source share

This is a verified error. I sent Oracle information that their sample code was not working using a standard installation of JDK 1.7.0 or JRE7. Using the source code below, compiled in TranslucentWindow.java, it fails and throws an exception, originally mentioned above.

From Oracle Transucency / Shaped Windows Web Page

 // Taken from http://download.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html#uniform import java.awt.*; import javax.swing.*; import static java.awt.GraphicsDevice.WindowTranslucency.*; public class TranslucentWindow extends JFrame { public TranslucentWindow() { super("TranslucentWindow"); setLayout(new GridBagLayout()); setSize(300,200); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Add a sample button. add(new JButton("I am a Button")); } public static void main(String[] args) { // Determine if the GraphicsDevice supports translucency. GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); //If translucent windows aren't supported, exit. if (!gd.isWindowTranslucencySupported(TRANSLUCENT)) { System.err.println("Translucency is not supported"); System.exit(0); } // Create the GUI on the event-dispatching thread SwingUtilities.invokeLater(new Runnable() { @Override public void run() { TranslucentWindow tw = new TranslucentWindow(); // Set the window to 55% opaque (45% translucent). tw.setOpacity(0.55f); // Display the window. tw.setVisible(true); } }); } 

}

 Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is decorated at java.awt.Frame.setOpacity(Frame.java:960) at TranslucentWindow$1.run(TranslucentWindow.java:38) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705) at java.awt.EventQueue.access$000(EventQueue.java:101) at java.awt.EventQueue$3.run(EventQueue.java:666) at java.awt.EventQueue$3.run(EventQueue.java:664) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:675) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105) at java.awt.EventDispatchThread.run(EventDispatchThread.java:90) 
+3
source share

Use com.sun.awt.AWTUtilities.setWindowOpacity(w, 0.5f) in JDK 7.

See here .

+2
source share

Hi, the problem with this code is that the following line of code is missing in the main () method:

 JFrame.setDefaultLookAndFeelDecorated(true); 

It should go right after the code that checks to see if the translucent windows are transparent and exit:

  //If translucent windows aren't supported, exit. if (!gd.isWindowTranslucencySupported(TRANSLUCENT)) { System.err.println( "Translucency is not supported"); System.exit(0); } JFrame.setDefaultLookAndFeelDecorated(true); // Create the GUI on the event-dispatching thread SwingUtilities.invokeLater(new Runnable() { @Override public void run() { TranslucentWindow tw = new TranslucentWindow(); // Set the window to 55% opaque (45% translucent). tw.setOpacity(0.55f); // Display the window. tw.setVisible(true); } }); 

Also, the image of a homogeneous translucent image is misleading as it uses the look of java. Instead, the image should use the look of the Windows system (assuming you are on Windows). If you try to use the appearance of Java (i.e. JFrame.setDefaultLookAndFeelDecorated(false); ) then it will cause the same error as before. I was hoping the translucent window would work with the look of Java, but I don't think it is possible.

0
source share

I think you need to install Undecorated before setBackground, this will fix the problem.

0
source share

All Articles