(This problem occurs on Mac OS X 10.10 using Java 1.7.0_45. On Windows 7 using Java 1.7.0_55 I do not have this problem.)
I have a translucent "always on top" JFrame with some information in a JLabel. When the information changes, I inform the JFrame to pack() again, to keep the JFrame as small as possible. When the JFrame re-lays out and paints the JFrame after the resize, there are rendering glitches.
Here's a SSCCE:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ScratchSpace {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final JLabel label = new JLabel("Test message that is long");
label.setOpaque(false);
label.setForeground(Color.WHITE);
JLabel iconLabel = new JLabel("Label 2");
JPanel contentPane = new JPanel();
contentPane.setOpaque(false);
contentPane.add(label);
contentPane.add(iconLabel);
final JFrame hudFrame = new JFrame();
hudFrame.setAlwaysOnTop(true);
hudFrame.setType(Window.Type.UTILITY);
hudFrame.setUndecorated(true);
hudFrame.setBackground(new Color(0, 0, 0, 50));
hudFrame.setContentPane(contentPane);
hudFrame.pack();
hudFrame.setLocationRelativeTo(null);
hudFrame.setVisible(true);
Timer timer = new Timer(2000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setText("Shorter test message");
hudFrame.pack();
}
});
timer.setRepeats(false);
timer.start();
}
});
}
}
Here's a magnified screenshot of the output, in which you can see the JFrame has not re-rendered correctly. There is a pale impression of the JFrame's previous contents:
What can I do to fix this problem, while keeping per-pixel translucency?