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

JMenuItem text set to NULL if action command is set for buttons

XMLWordPrintable


      Issue with Java version 6u24

      JMenuItem text will be reset to null if an generic action command is set for
      buttons. It can be reproduced with following code.

      Test case:
      public class Start
      {

          JTree tree = new JTree();

          public Start() {
              tree.setEditable(true);
              tree.addMouseListener(new GenericMouseAdapter());

              JFrame test = new JFrame();
              test.add(new JScrollPane(tree));
              test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              test.setSize(500, 500);
              test.setLocationRelativeTo(null);
              test.setVisible(true);

          }

          private class GenericMouseAdapter extends MouseAdapter {

              @Override
              public void mousePressed(MouseEvent event) {

                  if (SwingUtilities.isRightMouseButton(event)) {

                      TreePath path = tree.getPathForLocation( event.getX(),
      event.getY());
                      if (path!=null) {
                          tree.setSelectionPath(path);

                          Action itemAction = new AbstractAction() {

                              @Override
                              public void actionPerformed(ActionEvent e) {
                                  System.out.println("Do nothing");
                              }
                          };
                          JPopupMenu menu = new JPopupMenu();

                          // copy
                          JMenuItem copy = new JMenuItem("Button 1");
                          copy.setAction(itemAction);
                          menu.add(copy);

                          menu.add(new JPopupMenu.Separator());

                          // find
                          JMenuItem find = new JMenuItem("Button 2");
                          find.setAction(itemAction);
                          menu.add(find);

                          // show popup
                          menu.setVisible(true);
                          menu.show(event.getComponent(), event.getX(),
      event.getY());
                      }
                  }
              }
          }

          public static void main(String[] args){
              new Start();
          }
      }

            dmarkov Dmitry Markov
            asaha Abhijit Saha
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: