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

[macosx] Dynamically created JMenuItems don't work on Java 9

XMLWordPrintable

    • 9
    • x86_64
    • os_x

      FULL PRODUCT VERSION :
      java version "9-ea"
      Java(TM) SE Runtime Environment (build 9-ea+146)
      Java HotSpot(TM) 64-Bit Server VM (build 9-ea+146, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      OS X 10.10.5
      #uname -a
      Darwin bastet.dyn.helios.de 14.5.0 Darwin Kernel Version 14.5.0: Thu Apr 21 20:40:54 PDT 2016; root:xnu-2782.50.3~1/RELEASE_X86_64 x86_64

      A DESCRIPTION OF THE PROBLEM :
      If creating JMenuItems on the fly with MenuListener.menuSelected, they are without function on Java 9 using the DesktopMenu. On previous java version it's working.

      REGRESSION. Last worked in version 8u102

      ADDITIONAL REGRESSION INFORMATION:
      java version "9-ea"
      Java(TM) SE Runtime Environment (build 9-ea+146)
      Java HotSpot(TM) 64-Bit Server VM (build 9-ea+146, mixed mode)


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the submitted program with some item labels as arguments. Compare the behavior between the first and the second menu.
      Run the same program on Java 1.6 to Java 1.8

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The JMenuItems created from the arg list should behave the same on first and the second menu.
      ACTUAL -
      The JMenuItems created on the fly are without function, while the precreated "Exit" item works.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      /*
       * Testprogram for dynamically created MenuItems.
       * Created from Netbeans Tue Nov 29 08:11:57 UTC 2016
       *
       */

      package java9menu;

      import java.awt.EventQueue;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import javax.swing.AbstractAction;
      import javax.swing.Action;
      import javax.swing.GroupLayout;
      import javax.swing.JFrame;
      import javax.swing.JMenu;
      import javax.swing.JMenuBar;
      import javax.swing.JMenuItem;
      import javax.swing.JOptionPane;
      import javax.swing.JPopupMenu;
      import javax.swing.WindowConstants;
      import javax.swing.event.MenuEvent;
      import javax.swing.event.MenuListener;

      public class Main extends JFrame {
      public Main() {
      initComponents();
      jMenu1.add(exitApplication);
      jMenu1.addSeparator();
      }

      /** This method is called from within the constructor to
      * initialize the form.
      * WARNING: Do NOT modify this code. The content of this method is
      * always regenerated by the Form Editor.
      */
      @SuppressWarnings( "unchecked" )
          // <editor-fold defaultstate="collapsed" desc="Generated Code">
          private void initComponents() {

              setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

              jMenu1.setText("File");
              jMenuBar1.add(jMenu1);

              jMenu2.setText("DynTest");
              jMenu2.addMenuListener(new MenuListener() {
                  public void menuSelected(MenuEvent evt) {
                      jMenu2MenuSelected(evt);
                  }
                  public void menuDeselected(MenuEvent evt) {
                      jMenu2MenuDeselected(evt);
                  }
                  public void menuCanceled(MenuEvent evt) {
                  }
              });

              jMenuItem1.setAction(exitApplication);
              jMenu2.add(jMenuItem1);
              jMenu2.add(jSeparator1);

              jMenuBar1.add(jMenu2);

              setJMenuBar(jMenuBar1);

              GroupLayout layout = new GroupLayout(getContentPane());
              getContentPane().setLayout(layout);
              layout.setHorizontalGroup(
                  layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                  .addGap(0, 400, Short.MAX_VALUE)
              );
              layout.setVerticalGroup(
                  layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                  .addGap(0, 279, Short.MAX_VALUE)
              );

              pack();
          }// </editor-fold>

          private void jMenu2MenuSelected(MenuEvent evt) {
      addMenuItems(jMenu2);
          }

          private void jMenu2MenuDeselected(MenuEvent evt) {
      for (int i = jMenu2.getMenuComponentCount(); i-- > 2;)
      jMenu2.remove(i);
          }

      public void addMenuItems(JMenu menu) {
      for (String text : names)
      menu.add(text).addActionListener(showClickActionListener);
      }
      String[] names;

      static final ActionListener showClickActionListener = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
      JMenuItem mi = (JMenuItem)evt.getSource();
      String s = mi.getText();
      JOptionPane.showMessageDialog(main, s + " clicked");
      }
      };
      static final Action exitApplication = new AbstractAction("Exit") {
      public void actionPerformed(ActionEvent evt) {
      System.exit(0);
      }
      };

      public static void main(final String args[]) {
      System.setProperty("apple.laf.useScreenMenuBar", "true");
      EventQueue.invokeLater(new Runnable() {
      public void run() {
      main = new Main();
      main.names = args;
      main.setVisible(true);
      main.addMenuItems(main.jMenu1);
      }
      });
      }
      static Main main;
          // Variables declaration - do not modify
          private final JMenu jMenu1 = new JMenu();
          private final JMenu jMenu2 = new JMenu();
          private final JMenuBar jMenuBar1 = new JMenuBar();
          private final JMenuItem jMenuItem1 = new JMenuItem();
          private final JPopupMenu.Separator jSeparator1 = new JPopupMenu.Separator();
          // End of variables declaration
      }

      ---------- END SOURCE ----------

            aniyogi Avik Niyogi (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: