For vextorspace, which claims:
JMenuBar can only be added to JFrames, JDialogs, and JApplets.
This example shows that JMenuBar is easy to add to a JPanel (or any container):
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import javax.swing.*; public class MenuBarEg { private static void createAndShowGui() { final JFrame frame = new JFrame("MenuBar Exampe"); JMenuItem barItem = new JMenuItem(new AbstractAction("Bar") { @Override public void actionPerformed(ActionEvent arg0) { JOptionPane.showMessageDialog(frame, "Hello from bar!"); } }); JMenu fooMenu = new JMenu("Foo"); fooMenu.add(barItem); JMenuBar menuBar = new JMenuBar(); menuBar.add(fooMenu); JPanel menuBarHoldingPanel = new JPanel(new BorderLayout()); menuBarHoldingPanel.add(menuBar, BorderLayout.PAGE_START); JPanel mainPanel = new JPanel(new GridLayout(0, 1));
Not only is this easy to do, there are many cases where this is desired.
source share