javaswingframejwindow

Any method like setOpacity that works with JWindow


I was just wondering if there was any method that was like setOpacity(float); that worked with the JWindow. I've tried the setOpacity method but it seems like it doesn't work with JWindows because I get the following error when trying to use the method

The method setOpacity(float) from the type Window is not visible

Here is the code in my main method.

JWindow window = new JWindow();
window.setSize(100.100);
window.setVisible(true);

Solution

  • The short answer is no. But there is something else you can do.

    Start by taking a look at How to Create Translucent and Shaped Windows

    enter image description here

    public class TranslucentWindow {
    
        public static void main(String[] args) {
            new TranslucentWindow();
        }
    
        public TranslucentWindow() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JWindow window = new JWindow();
                    window.setSize(100, 100);
                    window.setBackground(new Color(255, 0, 0, 128));
                    window.setLocationRelativeTo(null);
                    window.setVisible(true);
    
                }
            });
        }
    
    }
    

    Updated

    Confirmed operation under Windows 7 and MacOS X, Java 7

    enter image description here

    Updated

    Try and a check to see if translucent is actually supported...

    GraphicsEnvironment ge
            = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();
    
    boolean isUniformTranslucencySupported
            = gd.isWindowTranslucencySupported(TRANSLUCENT);
    boolean isPerPixelTranslucencySupported
            = gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT);
    boolean isShapedWindowSupported
            = gd.isWindowTranslucencySupported(PERPIXEL_TRANSPARENT);
    
    System.out.println("Hello");
    
    if (isUniformTranslucencySupported && isPerPixelTranslucencySupported && isShapedWindowSupported) {
    
        // Build window as normal...
    
    } else {
    
        if (!isUniformTranslucencySupported) {
            System.err.println("Translucentancy is not support");
        }
        if (!isPerPixelTranslucencySupported) {
            System.err.println("Per Pixel Translucentancy is not support");
        }
        if (!isShapedWindowSupported) {
            System.err.println("Per Pixel Transparenancy is not support");
        }
    
        System.exit(0);
    
    }