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

JPopupMenus keep previous max width when reused

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.3.0
    • client-libs



      Name: yyT116575 Date: 01/16/2001


      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      /*
      Under JRE 1.2.2, the following code works fine - each menu is displayed with the
       correct length. Under 1.3.0, the menu will retain it's max length from previous
       use. To show the problem, run the following and right-click a couple of times.

      The problem is that BasicMenuItemUI is leaving a the max text width behind in
      the popup menu's client properties hashtable.
      */

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;

      public final class PopupBug extends JFrame
      {
        private PopupBug()
        {
          super("Bug");
            
          setBounds(100, 100, 300, 200);
            
          //Quit when the window closes.
          addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
              System.exit(0);
            }
          });

          setBounds(100,100,400,300);
          setVisible(true);

          PopupManager.install();
        }

        public static void main(String[] args)
        {
          new PopupBug();
        }
            
        //----------------------------------------------------------------

        private static final class PopupManager implements AWTEventListener
        {
          private static PopupManager instance;
          private static boolean installed=false;
          private static final String[] demoText = new String[]{"A really long popupmenuline.....","short"};

          private final MyPopup popupInstance;
          private int demoCount = 0;

          public static void install()
          {
            if (!installed)
            {
              installed=true;
              instance = new PopupManager();
              Toolkit.getDefaultToolkit().addAWTEventListener(instance,AWTEvent.MOUSE_EVENT_MASK);
            }
          }

          private PopupManager()
          {
            popupInstance = new MyPopup();
          }

          public void eventDispatched(final AWTEvent e)
          {
            if (e instanceof MouseEvent)
            {
              //For some reason, mouse enter and mouse exit events are also
              //sometimes considered to be PopupTriggers...
              if (e.getID()==MouseEvent.MOUSE_RELEASED || e.getID()==MouseEvent.MOUSE_PRESSED)
              {
                if (((MouseEvent)e).isPopupTrigger())
                {
                  new Thread() {
                    public void run() {
                      getPopupMenu((MouseEvent)e);
                    }
                  }.start();
                }
              }
            }
          }

          private void getPopupMenu(MouseEvent me)
          {
            //if the pop-up menu is active, hide it
            if (popupInstance.isVisible())
              popupInstance.setVisible(false);

            //clear out the popup
            popupInstance.clear();

            popupInstance.add(new MyAction(demoText[demoCount++%2]));
            popupInstance.show((Component)me.getSource(), me.getX(), me.getY());
          }
        }

        //----------------------------------------------------------------
         
        private static final class MyPopup extends JPopupMenu
        {
          public void clear()
          {
            Component[] children = getComponents();
            for (int i=0 ; i<children.length ; i++)
              remove(children[i]);
               
            //This is the workaround
               
            //putClientProperty(/*javax.swing.plaf.basic.BasicMenuItemUI.MAX_TEXT_WIDTH*/"maxTextWidth", null);
          }
        }

        //----------------------------------------------------------------

        private static final class MyAction extends AbstractAction
        {
          MyAction(String text) { super(text); }
          public void actionPerformed(ActionEvent ae) {
            System.out.println("action"); }
        }
      }
      (Review ID: 115199)
      ======================================================================

            mdavidsosunw Mark Davidson (Inactive)
            yyoungsunw Yung-ching Young (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: