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

PopupMenu is not displayed when a TrayIcon is right-clicked

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version " 1.7.0_21 "
      Java(TM) SE Runtime Environment (build 1.7.0_21-b12)
      Java HotSpot(TM) 64-Bit Server VM (build 23.21-b01, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Mac OS X 10.8.0

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      iMac8,1
      2GB RAM

      A DESCRIPTION OF THE PROBLEM :
      When the user right-clicks on the installed TrayIcon, the PopupMenu that was passed to the TrayIcon constructor is not displayed. The PopupMenu is displayed only when the user left-clicks the TrayIcon.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1.) Create a TrayIcon with an associated PopupMenu
      2.) Insert TrayIcon into SystemTray instance
      3.) Attempt to right-click the displayed TrayIcon

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The user expects the PopupMenu to be displayed whenever they right *or* left click the TrayIcon, just as other icons in the notification area do on Mac OS X.
      ACTUAL -
      The PopupMenu was not displayed when the user right-clicked the TrayIcon. If the user right-clicks other non-Java icons in the notification area on Mac OS X, their associated popup menu's are displayed.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.AWTEvent;
      import java.awt.MenuItem;
      import java.awt.PopupMenu;
      import java.awt.SystemTray;
      import java.awt.Toolkit;
      import java.awt.TrayIcon;
      import java.awt.event.AWTEventListener;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.awt.event.MouseEvent;
      import java.awt.event.MouseListener;
      import javax.swing.ImageIcon;

      public class TrayTest
      {
        
        public static void main(String[] args ) throws Exception
        {
          MenuItem mi1 = new MenuItem( " Test Action 1 " );
          MenuItem mi2 = new MenuItem( " Test Action 2 " );
          MenuItem mi3 = new MenuItem( " Exit " );
          
          mi1.addActionListener( new ActionListener()
          {
            @Override
            public void actionPerformed( ActionEvent e )
            {
              System.out.println( " Test Action 1 - actionPerformed() " );
            }
          });
          
          mi2.addActionListener( new ActionListener()
          {
            @Override
            public void actionPerformed( ActionEvent e )
            {
              System.out.println( " Test action 2 - actionPerformed() " );
            }
          });
          
          mi3.addActionListener( new ActionListener()
          {
            @Override
            public void actionPerformed( ActionEvent e )
            {
              System.exit( 0 );
            }
          });
          
          PopupMenu pm = new PopupMenu();
          pm.add( mi1 );
          pm.add( mi2 );
          pm.addSeparator();
          pm.add( mi3 );
          
          ImageIcon icon = new ImageIcon( " ./icon.png " );
          TrayIcon trayIcon = new TrayIcon( icon.getImage(), " Test System Tray " , pm );
          SystemTray.getSystemTray().add( trayIcon );
        }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      You can install a custom MouseListener on the TrayIcon and trigger the PopupMenu to be displayed via the following example:

      import java.awt.AWTEvent;
      import java.awt.MenuItem;
      import java.awt.PopupMenu;
      import java.awt.SystemTray;
      import java.awt.Toolkit;
      import java.awt.TrayIcon;
      import java.awt.event.AWTEventListener;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.awt.event.MouseEvent;
      import java.awt.event.MouseListener;
      import javax.swing.ImageIcon;

      public class TrayTest
      {
        
        public static void main(String[] args ) throws Exception
        {
          MenuItem mi1 = new MenuItem( " Test Action 1 " );
          MenuItem mi2 = new MenuItem( " Test Action 2 " );
          MenuItem mi3 = new MenuItem( " Exit " );
          
          mi1.addActionListener( new ActionListener()
          {
            @Override
            public void actionPerformed( ActionEvent e )
            {
              System.out.println( " Test Action 1 - actionPerformed() " );
            }
          });
          
          mi2.addActionListener( new ActionListener()
          {
            @Override
            public void actionPerformed( ActionEvent e )
            {
              System.out.println( " Test action 2 - actionPerformed() " );
            }
          });
          
          mi3.addActionListener( new ActionListener()
          {
            @Override
            public void actionPerformed( ActionEvent e )
            {
              System.exit( 0 );
            }
          });
          
          PopupMenu pm = new PopupMenu();
          pm.add( mi1 );
          pm.add( mi2 );
          pm.addSeparator();
          pm.add( mi3 );
          
          ImageIcon icon = new ImageIcon( " ./icon.png " );
          final TrayIcon trayIcon = new TrayIcon( icon.getImage(), " Test System Tray " , pm );
          
          trayIcon.addMouseListener( new MouseListener()
          {

            @Override
            public void mouseClicked( MouseEvent e )
            {
              if ( e.getButton() == MouseEvent.BUTTON3 )
              {
                JFrame blank = new JFrame();
                blank.setUndecorated( true );
                blank.setVisible( true );
                blank.add( trayIcon.getPopupMenu() );
                trayIcon.getPopupMenu().show( blank, e.getX(), e.getY() );
              }
            }

            @Override
            public void mousePressed( MouseEvent e )
            {
            }

            @Override
            public void mouseReleased( MouseEvent e )
            {
            }

            @Override
            public void mouseEntered( MouseEvent e )
            {
            }

            @Override
            public void mouseExited( MouseEvent e )
            {
            }
            
          });
          SystemTray.getSystemTray().add( trayIcon );
        }
      }

            dnguyen Damon Nguyen
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: