Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4231737
Votes 0
Synopsis JDialogs not placed correctly when invoked from JPopups
Category java:classes_swing
Reported Against 1.2
Release Fixed
State 6-Fix Understood, request for enhancement
Priority: 4-Low
Related Bugs 6499857
Submit Date 21-APR-1999
Description


When a JDialog is popped up from a menu item, the popup cannot
be used as the component for setting the dialog location.

Specifically, I had an ActionListener to a JMenuItem that looks
like this:

   public void actionPerformed (ActionEvent e)
   {
      Component sourceComponent = null;

      if (e.getSource () instanceof Component)
         sourceComponent = (Component) e.getSource ();

      JOptionPane.showMessageDialog
         (sourceComponent,
         name + " " + versionNumber + "\n" + versionDate,
         "About " + name,
         JOptionPane.INFORMATION_MESSAGE);
   }

and the popup was always in the center of the screen.  I tracked
problem to where JDialog has a loop to find the window that
contains the component.  This logic does not allow for the
fact that JPopupMenus are not in the parent chain of their
windows.  I changed my code to the following, which worked fine.
I recommend this change to the JDialog logic.  Thank you.

New code:

   public void actionPerformed (ActionEvent e)
   {
      Component sourceWindow = null;

      if (e.getSource () instanceof Component)
         sourceWindow = getWindow ((Component) e.getSource ());

      JOptionPane.showMessageDialog
         (sourceComponent,
         name + " " + versionNumber + "\n" + versionDate,
         "About " + name,
         JOptionPane.INFORMATION_MESSAGE);
   }

   Component getWindow (Component c)
   {
      // This is copied from JDialog, except that it fixes
      // the bug caused by JPopupMenu

      boolean   trace = true;
      Component root = null;

      if (c != null)
      {
         if (c instanceof Window || c instanceof Applet)
         {
            if (trace)
               System.out.println (c.getClass ().getName ());
            root = c;
         }
         else
         {
            Component parent;
            for (parent = getParent (c);
                 parent != null ;
                 parent = getParent (parent))
            {
               if (trace && parent == null)
                  System.out.println ("null");
               else if (trace)
                  System.out.println (parent.getClass ().getName ());

               if (parent instanceof Window || parent instanceof Applet)
               {
                  root = parent;
                  break;
               }
            }
         }
      }
      else if (trace)
         System.out.println ("null");

      return root;
   }

   Component getParent (Component c)
   {
      if (c instanceof JPopupMenu)
         return ((JPopupMenu) c).getInvoker ();
      else
         return c.getParent ();
   }
(Review ID: 53237) 
======================================================================
Posted Date : 2005-11-10 13:59:58.0
Work Around




See above.
======================================================================
Evaluation
It's possible to change JOptionPane to fix this. We need to use the invoker of the popup menu instead of popup menu itself for setting location of JOptionPane.
  xxxxx@xxxxx   2005-03-17 18:15:13 GMT
Also we should modify JOptionPane.getFrameForComponent() and JOptionPane.getWindowForComponent() methods because then we invoke JOptionPane from JPopupMenu, these methods should return frame/window not for popup menu, but for it's invoker.
  xxxxx@xxxxx   2005-07-04 19:28:48 GMT
Posted Date : 2005-07-04 19:28:48.0
Comments
  
  Include a link with my name & email   

Submitted On 27-OCT-2006
CurtCox
See also:

Hans Muller's Blog: Dialog Diatribe
http://weblogs.java.net/blog/hansmuller/archive/2006/10/dialog_diatribe.html


Submitted On 06-DEC-2006
MiguelM
This bug comes up anywhere you write a loop based on parent.getParent(). This includes these 4 methods in SwingUtilities:

Window getWindowAncestor(Component c) 
Window windowForComponent(Component c) 
Component getRoot(Component c) 
JRootPane getRootPane(Component c)

I'm sure many developers have the same kind of loop somewhere in their code. I know I do.

See the second comment to bug 6499857 for a suggested change that would fix all of these loops.



PLEASE NOTE: JDK6 is formerly known as Project Mustang