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

JPopupMenu.show() method does not work sometimes.

XMLWordPrintable

    • sparc
    • solaris_2.6



      Name: aaC67449 Date: 10/28/99


      JPopupMenu.show(Component, int, int) method does not show popup sometimes.
      See example1.

      The main thread of example is synchronized with the Event-Dispatching Thread by
      using SwingUtilities.invokeAndWait() method, so all post-initialization GUI work
      is done in the Event-Dispatching Thread, as described in
      "The Java Tutorial. Threads and Swing"
      The example shows the frame, after this tries to show JPopupMenu (the frame is
      invoker) and check the popup visibility, then do it again till the the check
       will failed.
      The loop is used because sometimes test should be run more then one time to
      reproduce failure
      When the test failed, you can see the frame without popup at the screen and "isShowing returns false. Should be true" text in the stdout.

      The same problem is in the JComboBox.setPopupVisible() method.
      See example2 that reproduce this problem in same way.

      ------------------------- example1 ----------------
      import javax.swing.*;
      import java.awt.*;

      public class Test{

          public static void main(String argv[]) {
       
              final JPopupMenu c = new JPopupMenu ();
              ShowVerifier sv = new ShowVerifier(c);
              c.add(new JMenuItem("Test"));

              final JFrame f= new JFrame();
              f.setSize(100,100);

              while (true) {
                  f.setVisible(true);
       
                  // synchronously call show()
                  try {
                      SwingUtilities.invokeAndWait(new Runnable() {
                          public void run() {
                              c.show(f, 10, 10);
                          }
                      });
                  } catch (Exception e) {
                      e.printStackTrace();
                  }

                  // synchronously call isShowing()
                  try {
                      SwingUtilities.invokeAndWait(sv);
                  } catch (Exception e) {
                      e.printStackTrace();
                  }

                  // verify value returned by isShowing()
                  if(!sv.isShowing()) {
      System.out.println("isShowing returns "+sv.isShowing()
      + ". Should be true");
                      break;
                  }


                  // synchronously call setvisible(false)
                  try {
                      SwingUtilities.invokeAndWait(new Runnable() {
                          public void run() {
                              c.setVisible(false);
                          }
                      });
                  } catch (Exception e) {
                      e.printStackTrace();
                  }

                  // synchronously call isShowing()
                  try {
                      SwingUtilities.invokeAndWait(sv);
                  } catch (Exception e) {
                      e.printStackTrace();
                  }

                  // verify value returned by isShowing()
                  if(sv.isShowing()) {
      System.out.println("isShowing returns "+sv.isShowing()
      + ". Should be false");
                  }
                  f.dispose();

               }
           }
      }


      class ShowVerifier implements Runnable {
         boolean shown = false;
         JPopupMenu comp = null;
         public ShowVerifier (JPopupMenu c) {
             comp = c;
         }

         synchronized public void run() {
              shown=comp.isShowing();
         }

         synchronized boolean isShowing() {
             return shown;
         }
      }

      -------------------- output -------------------
      isShowing returns false. Should be true

      ------------------------- example2 ----------------
      import javax.swing.*;
      import java.awt.*;

      public class Test{

          public static void main(String argv[]) {
       
              JFrame f= new JFrame();
              final JComboBox c = new JComboBox ();
              ShowVerifier sv = new ShowVerifier(c);
              c.addItem("Test");
           

              while (true) {
                  f.getContentPane().add(c);
                  f.pack();
                  f.setVisible(true);
       
                  // synchronously call setPopupVisible()
                  try {
                      SwingUtilities.invokeAndWait(new Runnable() {
                          public void run() {
                              c.setPopupVisible(true);
                          }
                      });
                  } catch (Exception e) {
                      e.printStackTrace();
                  }

                  // synchronously call getPopupVisible()
                  try {
                      SwingUtilities.invokeAndWait(sv);
                  } catch (Exception e) {
                      e.printStackTrace();
                  }

                  // verify value returned by getPopupVisible()
                  if(!sv.isShowing()) {
      System.out.println("PopupVisible is "+sv.isShowing()
      + ". Should be true");
                      break;
                  }


                  // synchronously call setPopupVisible()
                  try {
                      SwingUtilities.invokeAndWait(new Runnable() {
                          public void run() {
                              c.setPopupVisible(false);
                          }
                      });
                  } catch (Exception e) {
                      e.printStackTrace();
                  }

                  // synchronously call getPopupVisible()
                  try {
                      SwingUtilities.invokeAndWait(sv);
                  } catch (Exception e) {
                      e.printStackTrace();
                  }

                  // verify value returned by getPopupVisible()
                  if(sv.isShowing()) {
      System.out.println("PopupVisible is "+sv.isShowing()
      + ". Should be false");
                  }
                  f.dispose();

               }
           }
      }


      class ShowVerifier implements Runnable {
         boolean shown = false;
         JComboBox comp = null;
         public ShowVerifier (JComboBox c) {
             comp = c;
         }

         synchronized public void run() {
              shown=comp.isPopupVisible();
         }

         synchronized boolean isShowing() {
             return shown;
         }
      }

      -------------------- output -------------------
      PopupVisible is false. Should be true


      ======================================================================

            rraysunw Richard Ray (Inactive)
            alisunw Ali Ali (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: