2

I've added a JMenu to an undecorated JFrame and the JMenuItem is not painted until I move the mouse over the unpainted area. Has anyone seen this problem and know how to fix/circumvent? Here is a reduced test case showing the problem.

import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class TestCase {
public static void main(String[] args) throws Exception {
    new JFrame() {{
        setJMenuBar(new JMenuBar() {{
            setOpaque(true);
            add(new JMenu("ProblemMenu") {{
                setOpaque(true);
                add(new JMenuItem("NotPainted"){{
                    setOpaque(true);
                }});}});}});
        setUndecorated(true);
        setBackground(new Color(11,111,222,196));
        setSize(300,300);
        setLocation(300,300);
        getContentPane().setLayout(new FlowLayout());
        getContentPane().add(new JButton(" OpaqueButton "));
        setVisible(true);
    }};
}
}

enter image description here

Click the menu "ProblemMenu" and the menu will open with a white rectangle where the JMenuItem should be located. Move the mouse over that area and the JMenuItem paints.

Java42
  • 7,628
  • 1
  • 32
  • 50
  • `}});}});}});` ..Huh? I for one find code a lot easier to understand when closing `}` are on their own line, and only one `{` is used per line. – Andrew Thompson Oct 17 '12 at 00:18
  • Try `SwingUtilities.invokeLater` to initialize your UI, see [Initial Threads](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html) for more details. – tenorsax Oct 17 '12 at 00:24
  • agree...I did it for compactness...apply your eclipse formatter. – Java42 Oct 17 '12 at 00:26
  • @max - I tried invokeLater() - no help...same issue. – Java42 Oct 17 '12 at 00:27

2 Answers2

1

The per-pixel translucent JFrame menu paint problem was caused by the JRE and fixed by moving to a newer JRE ( 1.7.0_09-b05 ).

Java42
  • 7,628
  • 1
  • 32
  • 50
1

It's probably setOpaque() calls in combination with your LAF.

Read this great answer for why setOpaque() is problematic.

setOpaque(true/false); Java

Community
  • 1
  • 1
chubbsondubs
  • 37,646
  • 24
  • 106
  • 138