JDK-6575395 : WindowsPopupMenuSeparatorUI does not paint background correctly
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 5.0,6
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2007-06-29
  • Updated: 2021-07-13
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_01"
Java(TM) SE Runtime Environment (build 1.6.0_01-b06)
Java HotSpot(TM) Client VM (build 1.6.0_01-b06, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
In WindowsPopupMenuSeparatorUI.paint(), only one line below the separator line is painted, instead of the region above and below the separator line.

    public void paint(Graphics g, JComponent c) {
        Dimension s = c.getSize();
	int y = s.height / 2;

	g.setColor(c.getForeground());
	g.drawLine(1, y - 1, s.width - 2, y - 1); // <- only one line is painted

	g.setColor(c.getBackground());
	g.drawLine(1, y,     s.width - 2, y);
    }

This results in a problem that setting the background of the separator only has effect on that one single line. The remaining region stays white.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the example code and click on the menu.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The menu items and separator all show a cyan background.
ACTUAL -
The separator shows a white background.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.*;
import javax.swing.*;

public class PopupMenuBackground
{
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                    final Color bg = Color.CYAN;
                    
                    JMenu menu = new JMenu("Menu");
                    JMenuItem item = new JMenuItem("item");
                    item.setBackground(bg);
                    menu.add(item);

                    JPopupMenu.Separator sep = new JPopupMenu.Separator();
                    sep.setBackground(bg);
                    menu.add(sep);
                    
                    item = new JMenuItem("item");
                    item.setBackground(bg);
                    menu.add(item);
        
                    JMenuBar bar = new JMenuBar();
                    bar.add(menu);
                    
                    JFrame frame = new JFrame();
                    frame.setJMenuBar(bar);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setVisible(true);
                }
                catch( Exception e )
                {
                    e.printStackTrace();
                }
            }
        });
    }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Extend WindowsPopupMenuSeparatorUI and paint correctly:

MySeparatorUI extends com.sun.java.swing.plaf.windows.WindowsPopupMenuSeparatorUI
{
    public void paint(Graphics g, JComponent c)
    {
        Dimension s = c.getSize();
        int y = s.height / 2;

        g.setColor(c.getBackground());
        g.fillRect(0, 0, s.width, s.height);

        g.setColor(c.getForeground());
        g.drawLine(1, y - 1, s.width - 2, y - 1);
    }
}