0

I am writing a desktop app with Swing. My UI's LookAndFeel is set to NimbusLookAndFeel using:

for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }

I am trying to set the background of a JDialog to transparent so that some animations drawn on a JPanel will show over the transparent JDialog.

To achieve this, I have used:

 JDialog dialog = new JDialog(frame, "Loading...", false);


        dialog.getRootPane().setOpaque(false);
       dialog.getContentPane().setBackground(new Color(0, 0, 0, 0));
        dialog.setBackground(new Color(0, 0, 0, 0));

I have also set the child JPanel's opaque property to false.

Unfortunately, a background which I have traced to the JDialog keeps appearing only when the NimbusLookAndFeel is used.

If I do not use NimbusLookAndFeel, it works, as the JDialog's background disaappears.

Here is code that reproduces this problem:

    package com.examples.me;


    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.WindowConstants;



public class Example extends JPanel {

    JDialog dialog;

    public Example(JFrame frame, boolean withNimbus) {

        if (withNimbus) {
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());

                        break;
                    }
                }
            } catch (Exception ex) {
                java.util.logging.Logger.getLogger(Example.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }

        }

        setOpaque(false);

        setSize(420, 350);

        dialog = new JDialog(frame, "Loading...", false);

        dialog.add(Example.this);
        dialog.setAlwaysOnTop(true);
        dialog.setResizable(false);
        dialog.setUndecorated(true);

        dialog.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);

        dialog.setSize(getSize());
        dialog.setLocationRelativeTo(null);

        dialog.getRootPane().setOpaque(false);
        dialog.getContentPane().setBackground(new Color(0, 0, 0, 0));
        dialog.setBackground(new Color(0, 0, 0, 0));

    }

    @Override
    protected void paintComponent(Graphics gg) {
        super.paintComponent(gg);
        int w = getWidth();
        int h = getHeight();

        Graphics2D g = (Graphics2D) gg;

        g.setStroke(new BasicStroke(2));
        g.setColor(Color.red);
        g.drawRoundRect(1, 1, w - 2, h - 2, 64, 64);

        g.drawLine(1, h / 2, w - 2, h / 2);

        g.drawLine(w / 2, 1, w / 2, h - 2);

    }

    @Override
    public boolean isVisible() {
        if (dialog != null) {
            return super.isVisible() && dialog.isVisible();
        }

        return super.isVisible();

    }

    @Override
    public void setVisible(boolean aFlag) {
        super.setVisible(aFlag); //To change body of generated methods, choose Tools | Templates.
        if (dialog != null) {
            dialog.setVisible(aFlag);
        }

    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {

                JFrame frame = new JFrame("Nimbus Failure Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(700, 450);

                Example p = new Example(frame , true);

                // p.setSize(500, 400);
                // f.add(p);
                frame.setLocationRelativeTo(null);

                p.setVisible(true);

                frame.setVisible(true);

            }
        });

    }

}

To see that it works normally without NimbusLookAndFeel, set the withNimbus parameter to false in the constructor. That is the expected behaviour.

How do I make a JDialog transparent when I am using a Nimbus look-and-feel?

Thanks.

gbenroscience
  • 994
  • 2
  • 10
  • 33
  • 1
    See [How to Create Uniform Transparency](https://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html#uniform). If you need more help post a proper [mre] demonstrating the problem. – camickr Nov 13 '19 at 01:05
  • @camickr, thanks for the heads-up. I had seen that tutorial, previously. Setting the opacity however not only fades out the JDialog, it also fades out the JPanel child on it. So this is not a solution to the issue – gbenroscience Nov 13 '19 at 01:13
  • @camickr , I have posted the example as you requested. Thanks – gbenroscience Nov 13 '19 at 01:33
  • I made a little edit to allow easy behavioural check when Nimbus is used versus when Nimbus is not used. – gbenroscience Nov 13 '19 at 01:39
  • Not sure how to test. All I did was change the boolean value in the Example constructor from true to false. I see the same behaviour in both cases. I see red rectangle on top and a frame underneath that I can drag around. The dialog is transparent in both cases. I also tested the `GradientTranslucentWindowDemo` from the tutorial. I supports transparency with a single statement to set the background of the frame transparent. I'm using JDK8 on Windows 7. – camickr Nov 13 '19 at 01:55
  • I am using JDK12 on MacOS Mojave. When I set the boolean to true, and I drag the frame away, I see the red rectangle and an off white background(the JDialog) underneath. If false, the red rectangle will be on a transparent background. Some research(see https://stackoverflow.com/a/2452381/1845404) shows that the problem is caused by the NimbusLookAndFeel not playing nicely with the setOpaque(args) method. So I took a different direction and solved the issue by borrowing ideas from https://stackoverflow.com/a/11873067/1845404 – gbenroscience Nov 13 '19 at 02:06

0 Answers0