Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6575395

WindowsPopupMenuSeparatorUI does not paint background correctly

    Details

      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);
          }
      }

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                serb Sergey Bylokhov
                Reporter:
                ryeung Roger Yeung (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Imported:
                  Indexed: