// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@

package example;

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

public final class MainPanel extends JPanel {
  private MainPanel() {
    super(new BorderLayout());
    // frame.setJMenuBar(createMenuBar());
    // addComponentListener(new ComponentAdapter() {
    //   @Override public void componentResized(ComponentEvent e) {
    //     System.out.println("componentResized");
    //     ((JComponent) e.getSource()).revalidate();
    //   }
    // });
    // frame.addWindowStateListener(new WindowStateListener() {
    //   @Override public void windowStateChanged(WindowEvent e) {
    //     EventQueue.invokeLater(new Runnable() {
    //       @Override public void run() {
    //         System.out.println("windowStateChanged");
    //         JFrame f = (JFrame) e.getWindow();
    //         ((JComponent) f.getContentPane()).revalidate();
    //       }
    //     });
    //   }
    // });
    add(createMenuBar(), BorderLayout.NORTH);
    add(new JScrollPane(new JTextArea()));
    setPreferredSize(new Dimension(320, 240));
  }

  private static JMenuBar createMenuBar() {
    JMenuBar menuBar = new JMenuBar();
    menuBar.setLayout(new FlowLayout(FlowLayout.LEFT, 2, 2) {
      @Override public Dimension preferredLayoutSize(Container target) {
        synchronized (target.getTreeLock()) {
          int targetWidth = target.getSize().width;
          targetWidth = targetWidth == 0 ? Integer.MAX_VALUE : targetWidth;
          Insets insets = target.getInsets();
          int hgap = getHgap();
          int vgap = getVgap();
          int maxWidth = targetWidth - insets.left - insets.right;
          int height = vgap;
          int rowWidth = hgap;
          int rowHeight = 0;
          int nmembers = target.getComponentCount();
          for (int i = 0; i < nmembers; i++) {
            Component m = target.getComponent(i);
            if (m.isVisible()) {
              Dimension d = m.getPreferredSize();
              if (rowWidth + d.width > maxWidth) {
                height += rowHeight;
                rowWidth = hgap;
                rowHeight = 0;
              }
              rowWidth += d.width + hgap;
              rowHeight = Math.max(rowHeight, d.height + vgap);
            }
          }
          height += rowHeight + insets.top + insets.bottom;
          return new Dimension(targetWidth, height);
        }
      }
      // // https://tips4java.wordpress.com/2008/11/06/wrap-layout/
      // // WrapLayout.java
      // // Rob Camick on November 6, 2008
      // private Dimension preferredLayoutSize;
      // @Override public void layoutContainer(Container target) {
      //   Dimension size = preferredLayoutSize(target);
      //   if (size.equals(preferredLayoutSize)) {
      //     super.layoutContainer(target);
      //   } else {
      //     preferredLayoutSize = size;
      //     Container top = target;
      //     while (!(top instanceof Window) && top.getParent() != null) {
      //       top = top.getParent();
      //     }
      //     top.validate();
      //   }
      // }
    });
    for (String key: new String[] {"File", "Edit", "Aaaaaaaaaaaaaaaaaaaaa", "Bbbbbbbbbbbbb", "Help"}) {
      menuBar.add(createMenu(key));
    }
    return menuBar;
  }

  private static JMenu createMenu(String key) {
    JMenu menu = new JMenu(key);
    menu.add("dummy1");
    menu.add("dummy2");
    menu.add("dummy3");
    return menu;
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(MainPanel::createAndShowGui);
  }

  private static void createAndShowGui() {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
      ex.printStackTrace();
      Toolkit.getDefaultToolkit().beep();
    }
    JFrame frame = new JFrame("@title@");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(new MainPanel());
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

@@ -113,16 +113,48 @@
 .awt.*;%0A
+import java.util.stream.Stream;%0A
 import j
@@ -273,51 +273,8 @@
 ));%0A
-    // frame.setJMenuBar(createMenuBar());%0A
     
@@ -686,65 +686,13 @@
 ter(
-new Runnable() %7B%0A    //       @Override public void run()
+() -%3E
  %7B%0A 
@@ -692,26 +692,24 @@
  -%3E %7B%0A    //
-  
        Syste
@@ -747,26 +747,24 @@
 ed%22);%0A    //
-  
        JFram
@@ -805,32 +805,16 @@
 //      
-   ((JComponent)
  f.getCo
@@ -824,17 +824,16 @@
 ntPane()
-)
 .revalid
@@ -843,23 +843,8 @@
 ();%0A
-    //       %7D%0A
     
@@ -884,47 +884,83 @@
     
-add(createMenuBar(), BorderLayout.NORTH
+SwingUtilities.invokeLater(() -%3E getRootPane().setJMenuBar(createMenuBar())
 );%0A 
@@ -3076,94 +3076,98 @@
     
-for (String key: new String%5B%5D %7B%22File%22, %22Edit%22, %22Aaaaaaaaaaaaaaaaaaaaa%22, %22Bbbbbbbbbbbbb
+Stream.of(%22File%22, %22Edit%22, %22View%22, %22Navigate%22, %22Code%22, %22Analyze%22, %22Refactor%22, %22Build%22, %22Run
 %22, %22
@@ -3175,54 +3175,68 @@
 elp%22
-%7D) %7B%0A
+)%0A  
       
-menuBar.add(createMenu(key));%0A    %7D
+.map(MainPanel::createMenu).forEach(menuBar::add);
 %0A   

use JRootPane#setJMenuBar(menuBar) instead of JPanel#add(menuBar, BorderLayout.NORTH)