Java tutorial
//package com.java2s; import java.util.ArrayList; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; public class Main { public static void addMenuAt(JFrame frame, JMenu newMenu, int index) { frame.setJMenuBar(addMenuAt(newMenu, frame.getJMenuBar(), index)); } /** * @param newMenu * @param menuBar * @param index * @return The same JMenuBar, for cascading. * TODO See if the same thing can be done with Container.add( component, index ) */ public static JMenuBar addMenuAt(JMenu newMenu, JMenuBar menuBar, int index) { ArrayList menuList = new ArrayList(); for (int i = 0; i < menuBar.getMenuCount(); i++) { if (i == index) { menuList.add(newMenu); } menuList.add(menuBar.getMenu(i)); } menuBar.removeAll(); // menuBar = new JMenuBar(); for (int i = 0; i < menuList.size(); i++) { JMenu menu = (JMenu) menuList.get(i); menuBar.add(menu); } return menuBar; } }