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

JDialogs not placed correctly when invoked from JPopups

    XMLWordPrintable

Details

    • Fix Understood
    • generic
    • generic

    Description

      Name: skT88420 Date: 04/21/99


      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)
      ======================================================================

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              skonchad Sandeep Konchady
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Imported:
                Indexed: