United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6421284 JPopupMenu behaves incorrectly with invoker being set to null
JDK-6421284 : JPopupMenu behaves incorrectly with invoker being set to null

Details
Type:
Bug
Submit Date:
2006-05-03
Status:
Resolved
Updated Date:
2011-01-19
Project Name:
JDK
Resolved Date:
2007-04-03
Component:
client-libs
OS:
generic,windows_xp
Sub-Component:
javax.swing
CPU:
x86,generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
5.0,6
Fixed Versions:

Related Reports
Backport:
Duplicate:
Relates:
Relates:
Relates:

Sub Tasks

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
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
                                     
2006-05-03
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.
                                     
2006-05-05
EVALUATION

We just need to fix it
                                     
2006-06-28
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
                                     
2006-07-04
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
                                     
2006-07-12
EVALUATION

This fix was rolledback
see 6495511
                                     
2007-03-29



Hardware and Software, Engineered to Work Together