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

JPopupMenu is blocked when opened in a modal Dialog

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      java -version:
      openjdk version "16.0.1" 2021-04-20
      OpenJDK Runtime Environment (build 16.0.1+9-24)
      OpenJDK 64-Bit Server VM (build 16.0.1+9-24, mixed mode, sharing)

      Windows:
      Windows 10 Pro for Workstations 20H2
      19042.1052


      A DESCRIPTION OF THE PROBLEM :
      When a JPopupMenu is displayed as a reaction to a user interaction in a modal dialog, the user can not interact with any components inside the JPopupMenu - It seems to be blocked because of the Dialog being modal. This is independent from allwing/disabling lightweight popups

      The same works flawlessly in Linux (Ubuntu 20.04)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run the attached class, click on the action and try to click any button in the resulting JPopupMenu:


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      It should be possible to interact with components in a JPopupMenu opened inside a modal Dialog
      ACTUAL -
      It is not possible to interact with components in the JPopupMenu opened inside a modal Dialog

      ---------- BEGIN SOURCE ----------
      import java.awt.*;
      import java.awt.event.ActionEvent;

      public class PopUpAction extends javax.swing.AbstractAction implements
      java.awt.event.WindowFocusListener
      ,java.awt.event.ActionListener
      {
      private static final int POPUPOFFSET=5;
      private javax.swing.JToolBar toolbar=new javax.swing.JToolBar();
      private javax.swing.JPopupMenu pmenu;
      private java.util.List<javax.swing.Action> actions;

      public PopUpAction()
      {
      super();
      init();
      }
      public PopUpAction(java.lang.String name)
      {
      super(name);
      init();
      }
      public PopUpAction(java.lang.String name, javax.swing.Icon icon)
      {
      super(name,icon);
      init();
      }
      private void init()
      {
      actions=new java.util.LinkedList();
      pmenu=new javax.swing.JPopupMenu();
      pmenu.setLightWeightPopupEnabled(false);
      }
      public javax.swing.AbstractButton add(javax.swing.Action action)
      {
      actions.add(action);
      javax.swing.AbstractButton btn=null;
      if(action.getValue(javax.swing.Action.SELECTED_KEY)!=null)
      {
      javax.swing.JToggleButton jtb=new javax.swing.JToggleButton(action);
      if(action.getValue(javax.swing.Action.SELECTED_KEY)!=null)
      {
      jtb.setSelectedIcon((javax.swing.Icon)action.getValue("SELECTED_ICON"));
      }
      btn=jtb;
      toolbar.add(jtb);
      }
      else
      btn=toolbar.add(action);
      pmenu.add(btn);
      btn.addActionListener(this);
      return btn;
      }
      public void setOrientation(int o)
      {
      if(pmenu!=null)
      pmenu.setVisible(false);
      toolbar.setOrientation(o);
      }
      public void windowGainedFocus(java.awt.event.WindowEvent e)
      {
      }
      public void windowLostFocus(java.awt.event.WindowEvent e)
      {
      if(pmenu!=null)
      pmenu.setVisible(false);
      }
      public void actionPerformed(java.awt.event.ActionEvent evt)
      {
      java.lang.Object source=evt.getSource();
      if(pmenu.isVisible()==true)
      pmenu.setVisible(false);
      else
      {
      try{
      java.awt.Component c=(java.awt.Component)evt.getSource();
      if(toolbar.getOrientation()==javax.swing.SwingConstants.VERTICAL)
      {
      pmenu.setLocation((int)(c.getLocationOnScreen().getX())+POPUPOFFSET,(int)(c.getLocationOnScreen().getY()+c.getHeight())-POPUPOFFSET);//,dialog.getWidth(),dialog.getHeight());
      }
      else
      {
      pmenu.setLocation((int)(c.getLocationOnScreen().getX()+c.getWidth())-POPUPOFFSET,(int)(c.getLocationOnScreen().getY())+POPUPOFFSET);//,dialog.getWidth(),dialog.getHeight());
      }
      pmenu.setVisible(true);
      pmenu.requestFocusInWindow();
      }
      catch(java.awt.IllegalComponentStateException exp){}
      }
      }
      public void clear()
      {
      actions.clear();
      toolbar.removeAll();
      }

      public boolean isPopupVisible()
      {
      return pmenu.isVisible();
      }

      public static void main(java.lang.String[] args)
      {
      java.awt.Dialog dialog=new javax.swing.JDialog((java.awt.Frame)null,"title",true);
      javax.swing.JPanel p=new javax.swing.JPanel(new java.awt.BorderLayout());
      javax.swing.JToolBar tb=new javax.swing.JToolBar();
      tb.setFloatable(false);
      p.add(tb, BorderLayout.NORTH);
      PopUpAction popUpAction=new PopUpAction("tata");
      popUpAction.add(new javax.swing.AbstractAction("huhu")
      {
      @Override
      public void actionPerformed(ActionEvent e)
      {

      }
      });
      popUpAction.add(new javax.swing.AbstractAction("hallo")
      {
      @Override
      public void actionPerformed(ActionEvent e)
      {

      }
      });

      tb.add(popUpAction);
      dialog.add(p);
      dialog.pack();
      dialog.setVisible(true);
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      There is none known

      FREQUENCY : always


            tr Tejesh R
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: