In BasicMenuItemUI we were (sometimes) painting the background of
menuitems with the wrong color and sometimes it shouldn't be painted at
all (making the underlying menu shine through). This is fixed with the
attached patch.
Interesting sidenote, Sun's impl doesn't seem to give
menuitem.getBackground() to paintMenuItem(), but instead the
selectionBackground. This is especially important for custom l&f
implementation that rely on this fact.

2006-03-28  Roman Kennke  <[EMAIL PROTECTED]>

        * javax/swing/plaf/basic/BasicMenuItemUI.java
        (paint): Call paintMenuItem with the selectionBackground as
        parameter.
        (paintBackground): Fixed the condition and color for the
background
        painting.

/Roman

Index: javax/swing/plaf/basic/BasicMenuItemUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicMenuItemUI.java,v
retrieving revision 1.43
diff -u -1 -0 -r1.43 BasicMenuItemUI.java
--- javax/swing/plaf/basic/BasicMenuItemUI.java	27 Jan 2006 10:19:59 -0000	1.43
+++ javax/swing/plaf/basic/BasicMenuItemUI.java	28 Mar 2006 10:33:04 -0000
@@ -534,49 +534,51 @@
   /**
    * Paints given menu item using specified graphics context
    * 
    * @param g
    *          The graphics context used to paint this menu item
    * @param c
    *          Menu Item to paint
    */
   public void paint(Graphics g, JComponent c)
   {
-    paintMenuItem(g, c, checkIcon, arrowIcon, c.getBackground(),
+    paintMenuItem(g, c, checkIcon, arrowIcon, selectionBackground,
                   c.getForeground(), defaultTextIconGap);
   }
 
   /**
    * Paints background of the menu item
    * 
    * @param g
    *          The graphics context used to paint this menu item
    * @param menuItem
    *          menu item to paint
    * @param bgColor
    *          Background color to use when painting menu item
    */
   protected void paintBackground(Graphics g, JMenuItem menuItem, Color bgColor)
   {
     // Menu item is considered to be highlighted when it is selected.
     // But we don't want to paint the background of JCheckBoxMenuItems
     ButtonModel mod = menuItem.getModel();
-    if (menuItem.isContentAreaFilled())
+    Color saved = g.getColor();
+    if (mod.isArmed() || ((menuItem instanceof JMenu) && mod.isSelected()))
       {
-        if ((menuItem.isSelected() && checkIcon == null) || (mod != null && 
-            mod.isArmed())
-            && (menuItem.getParent() instanceof MenuElement))
-          g.setColor(selectionBackground);
-        else
-          g.setColor(bgColor);
+        g.setColor(bgColor);
         g.fillRect(0, 0, menuItem.getWidth(), menuItem.getHeight());
-      } 
+      }
+    else if (menuItem.isOpaque())
+      {
+        g.setColor(menuItem.getBackground());
+        g.fillRect(0, 0, menuItem.getWidth(), menuItem.getHeight());
+      }
+    g.setColor(saved);
   }
 
   /**
    * Paints specified menu item
    * 
    * @param g
    *          The graphics context used to paint this menu item
    * @param c
    *          menu item to paint
    * @param checkIcon

Reply via email to