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

Key events for JPopup menu go to container instead

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.2.2
    • client-libs
    • generic, x86
    • generic, windows_nt



      Name: skT88420 Date: 06/17/99


      Compile the enclosed code. Right-mouse-click on the text field then type 'A' or 'B'. In a native windows application, the corresponding menu item will be selected. In swing, the 'A' or 'B' appears in the text field. I have tried other examples where the component underneath the popup is a JTree. The key event goes to the JTree not the popup. The bug happens with both lightweight and heavtweight popups and reproduces in jdk 1.2.2

      public class Test extends JFrame
      {

          JTextField text = new JTextField();
          JPopupMenu menu = new JPopupMenu();
          
          Test()
          {
              JMenuItem item;
              
              this.setBounds(0, 0, 500, 500);
              text.setBounds(10, 10, 100, 25);
              getContentPane().add(text);
               getContentPane().setLayout(null);
              
              item = menu.add(new Action1());
              item.setMnemonic('A');
              item = menu.add(new Action2());
              item.setMnemonic('B');
              
              text.addMouseListener(new MyAdapter());
              
          }
          
          public static void main(String args[])
          {
              try
                  {
                  UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                  }
              catch (Exception e)
                  {
                  e.printStackTrace();
                  }
              
              
              Test test = new Test();
              test.validate();
              test.setVisible(true);
              
          }
          
          class Action1 extends AbstractAction
          {
              
              Action1()
              {
                  
                  super("AAction");
                  
              }
              
              public void actionPerformed(ActionEvent event)
              {
                  
                  text.setText("AAction");
                  
              }
          }
          
          class Action2 extends AbstractAction
          {
              
              Action2()
              {
                  
                  super("BAction");
                  
              }
              
              public void actionPerformed(ActionEvent event)
              {
                  
                  text.setText("BAction");
                  
              }
          }
          
          class MyAdapter extends MouseAdapter
          {
              
              public void mousePressed(MouseEvent event)
              {
                  if (event.isMetaDown())
                      {
                      menu.show(text, event.getX(), event.getY());
                      }
                  
              }
              
          }
          
      }
      (Review ID: 84477)
      ======================================================================

      Name: krT82822 Date: 10/12/99


      If I have some code like this inside a JPanel
      -- begin --
      final JPopupMenu jpm = new JPopupMenu( "Label" );
      jpm.add( new JCheckBoxMenuItem( "Monday" ) );
      jpm.add( new JCheckBoxMenuItem( "Tuesday" ) );
      jpm.add( new JCheckBoxMenuItem( "Wednesday" ) );
      jpm.add( new JCheckBoxMenuItem( "Thursday" ) );
      jpm.add( new JCheckBoxMenuItem( "Friday" ) );
      jpm.add( new JCheckBoxMenuItem( "Saturday" ) );
      jpm.add( new JCheckBoxMenuItem( "Sunday" ) );

      final JButton jb2 = new JButton(jpm.getLabel(), new ImageIcon("images/tdown.gif"));
      jb2.setHorizontalTextPosition( jb2.LEADING );
      jpm.setInvoker(this);
      jb2.addActionListener( new AbstractAction()
      {
      public void actionPerformed( ActionEvent deed )
      {
      //SwingUtilities.invokeLater( new Runnable() { public void run() {
      jpm.show( jb2, 0,0 ); // } } );
      }
      } );
      add( jb2 );
      -- end --

      I cannot override any of the processKeyEvent methods, nor register keyboard actions, such that I can implement what
      seems like appropriate behavior for VK_UP, VK_DOWN, SPACE,
      TAB, and SHIFT_TAB.

      All I want to do is have a menu that has check boxes in it
      that can be driven by the keyboard, but the owner frame seems to get all keyboard/focus events.

      Suggestions?
      (Review ID: 96403)
      ======================================================================

            gsaab Georges Saab
            skonchad Sandeep Konchady
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: