Pergunta

There appears to be a bug with the window translucency functionality in Java 7 (I believe the problem existed in Java 6 as well). If I open a translucent window and then minimize its parent window, both disappear as you would expect. But then when you restore the parent window the translucent window never reappears. However, it is still there and will consume input as if nothing was wrong.

Here is my SSCCE:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class BrokenTranslucentWindow extends JApplet //same problem exists using a JFrame
{
    public BrokenTranslucentWindow()
    {
        JButton b = new JButton("Hello");
        b.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                JDialog d = new JDialog(Window.getWindows()[0])
                {
                    @Override
                    public void paint(Graphics g)
                    {
                        g.fillOval(0, 0, getWidth(), getHeight());
                    }
                };
                d.setUndecorated(true);
                d.setBackground(new Color(0, 0, 0, 0));
                d.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                d.setSize(300, 300);
                d.setLocationRelativeTo(null);
                // d.setModal(true);
                d.setVisible(true);
            }
        });
        this.add(b);
    }
}

You'll note that after you restore the parent window the translucent oval window will not be visible, but your cursor will still change to the hand cursor when you are over the area where the window should be.

I have submitted the bug to Oracle, but until it is fixed I could really use a better workaround.

My Question:

Does anybody have any ideas for a workaround that would prevent this from happening?

Fun Facts:

  • This causes big problems if the translucent window happened to be modal.
  • I'm focusing on per-pixel translucency, but the same applies to uniform translucency.
  • This problem presents with JFrames, Applets inside browsers, and Applets within the applet viewer.
Foi útil?

Solução

I have found one undesirable workaround. I post it in hopes that it will get some creative juices going. The workaround is to turn off the translucency when the window is deactivated, and turn it back on when it is activated.

d.addWindowListener(new WindowAdapter()
{
    public void windowActivated(WindowEvent e)
    {
        d.setBackground(new Color(0, 0, 0, 0));
    }

    public void windowDeactivated(WindowEvent e)
    {
        d.setBackground(Color.white);
    }
});

This isn't ideal because when you click off of the window it becomes opaque. However, it does prevent the window from disappearing forever.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top