JDK-6421284 : JPopupMenu behaves incorrectly with invoker being set to null
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 5.0,6
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic,windows_xp
  • CPU: generic,x86
  • Submitted: 2006-05-03
  • Updated: 2011-01-19
  • Resolved: 2007-04-03
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.
JDK 6
6 b92Fixed
Related Reports
Duplicate :  
Relates :  
Relates :  
Relates :  
Description
Run the following test (it is also attached to the bug report):

---- BEGIN OF SOURCE CODE ----

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class NullInvoker
{
  public static void main(String[] args)
  {
    final JFrame f = new JFrame("F");
    f.setBounds(100, 100, 100, 100);
    JButton b = new JButton("B");
    b.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        JPopupMenu p = new JPopupMenu();
        p.add(new JMenuItem("Item1"));
        p.add(new JMenuItem("Item2"));
        p.add(new JMenuItem("Item3"));
        // uncomment the next line to fix the problem
//        p.setInvoker(f);
        p.setLocation(300, 300);
        p.setVisible(true);
      }
    });
    f.add(b);
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    f.setVisible(true);
  }
}

---- END OF SOURCE CODE ----

When the test starts a frame with a button is shown. Click on the button to show JPopupMenu. It is not clickable and doesn't track any mouse move events to selected the proper menu item. After any item is clicked, popup is not closed.

Comments
EVALUATION This fix was rolledback see 6495511
29-03-2007

EVALUATION isPopupMenu() method used to return false for the null invoker, that's was the reason of incorrect selection behaviour, see JPopupMenu.setVisible() method (if it is not isPopupMenu() we don't set any initial selection) It also caused a problem when you click to the menu and it didn't hide, because JPopupMenu.menuSelectionChanged() hide the menu only if isPopupMenu returns true
12-07-2006

EVALUATION The problem appeared when we tried to add JPopupMenu to TrayIcon since no invoker was initially set, JPopupMenu behaviours really strange it was impossible to highlight or select any ot its menuItems Test case in the bug's description shows the same behaviour, setting invoker to any non-null Component fixes the problem Need to fix JPopupMenu.isPopupMenu() method
04-07-2006

EVALUATION We just need to fix it
28-06-2006

WORK AROUND I think the previous comment meant to say something like "this will be probably fixed with an upcoming Swing fix (6400183)", referring to a bug fix where Swing is fixing the problem with the autoshutdown now working. This is correct. However, we should still support JPopupMenu with a null invoker.
05-05-2006

WORK AROUND Setting any not-null value for invoker (see commented line in the test) fixes the problem. However, this leads to another problem: popup frame is not disposed and that prevents the whole Java application from autoshutdown (this will be probably fixed with
03-05-2006