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

Background color ignored for selected menu item in popup

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 6
    • client-libs

      FULL PRODUCT VERSION :
      java version "1.5.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
      Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode)

      java version "1.6.0-ea"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0-ea-b14)
      Java HotSpot(TM) Client VM (build 1.6.0-ea-b14, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows 2000 [Version 5.00.2195]

      A DESCRIPTION OF THE PROBLEM :
      Setting background color for menu item in popup menu that is currently selected has no visual effect (see code below, functions menuSelectionChanged and setArmed). However, if i don't call super() in menuSelectionChanged , then i do see the green background for the active entry.

      This was verified also for 1.4.2

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the attached code
      Right-click anywhere in the frame
      Move mouse over items

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Item under the mouse should be green, the other one should be red
      ACTUAL -
      Item under the mouse is grey, the other one is red

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.Color;
      import java.awt.Dimension;
      import java.awt.FlowLayout;
      import java.awt.event.MouseAdapter;
      import java.awt.event.MouseEvent;
      import java.awt.event.WindowAdapter;
      import java.awt.event.WindowEvent;

      import javax.swing.JFrame;
      import javax.swing.JMenuItem;
      import javax.swing.JPopupMenu;
      import javax.swing.border.BevelBorder;

      public class CheckPopup extends JFrame {
      class MyMenuItem extends JMenuItem {
      public void menuSelectionChanged(boolean isIncluded) {
      super.menuSelectionChanged(isIncluded);
      setOpaque(true);
      if (isIncluded)
      setBackground(Color.green);
      else
      setBackground(Color.red);
      System.out.println(isIncluded + ", " + this.getBackground());
      }

      public void setArmed(boolean b) {
      super.setArmed(b);
      setOpaque(true);
      if (b)
      setBackground(Color.green);
      else
      setBackground(Color.red);
      System.out.println(b + ", " + this.getBackground());
      }

      public void setEnabled(boolean isEnabled) {
      super.setEnabled(isEnabled);
      setOpaque(true);
      setBackground(Color.red);
      }
      }

          class MousePopupListener extends MouseAdapter {
           public MousePopupListener() {
           }
          
           public void mousePressed(MouseEvent e) { checkPopup(e); }
              public void mouseClicked(MouseEvent e) { checkPopup(e); }
              public void mouseReleased(MouseEvent e) { checkPopup(e); }

              private void checkPopup(MouseEvent e) {
                  if (e.isPopupTrigger( )) {
                   // create popup menu and start populating it
                   JPopupMenu popup = new JPopupMenu();
                      popup.setLabel("Operations");
                      popup.setBorder(new BevelBorder(BevelBorder.RAISED));
                      
                      MyMenuItem item1 = new MyMenuItem();
                      item1.setText("item 1");
                      item1.setBackground(Color.red);
                      popup.add(item1);
                      
                      MyMenuItem item2 = new MyMenuItem();
                      item2.setText("<html>item 2</html>");
                      item2.setBackground(Color.red);
                      popup.add(item2);
                      
                      popup.show(e.getComponent(), e.getX( ), e.getY( ));
                  }
              }
          }

          public CheckPopup() {
           super("check popup");
           this.getContentPane().setLayout(new FlowLayout());
      addMouseListener(new MousePopupListener());
      addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent e) {
                      System.exit(0);
                  }
              });
          }

          public static void main(String[] args) {
           CheckPopup cp = new CheckPopup();
           cp.setSize(new Dimension(100, 100));
           cp.setVisible(true);
          }
      }

      ---------- END SOURCE ----------
      ###@###.### 2004-12-16 21:17:11 GMT

            shickeysunw Shannon Hickey (Inactive)
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: