I've got a JMenuBar and a JPanel. I'd like to add the JMenuBar to the JPanel. How would I do so?
5 Answers
You can use a BorderLayout for your JPanel and put the JMenuBar into the NORTH area of the panel with
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(menubar, BorderLayout.NORTH);
JMenuBar is a JComponent and can be added to a Container like any other JComponent.

- 1,716
- 2
- 14
- 27
-
3I thought you can only add JMenuBar's to JFrame's – Enrique Nov 29 '10 at 00:55
-
Works, won't allow me to add it to the very top but close enough! – Skizit Nov 29 '10 at 12:10
-
BorderLayout.PAGE_START seemed to put it at the top for me. – Mike Bonnell Oct 22 '13 at 02:35
-
Is this code outdated? This does not work any more. – Alde Mar 18 '19 at 16:15
JMenuBars are set to the JFrame using the setJMenuBar method.
See the following tutorial on how to use them.
http://download.oracle.com/javase/tutorial/uiswing/components/menu.html

- 54,176
- 10
- 96
- 129
-
It's not possible to add them direct to a JPanel? I've got other items on there... if I add a JFrame wont it draw over my Panel items? – Skizit Nov 29 '10 at 00:01
-
2Well, a JMenuBar is just another JComponent, so you can add it to your JPanel just like you would anything else. However, as it is designed to be attached to a JFrame, it may not work correctly (or even at all!) – Codemwnci Nov 29 '10 at 00:07
I have another solution, although you have to add the JMenuBar in the "Other Components" in NetBeans (good enough). Create a JPanel and then add another JPanel inside (call it child) that fills the entire outter JPanel. Place your controls in the child panel. Then add the JMenuBar but NetBeans will place it in the "Other Components". Edit your source and in the ctor after it calls "initComponents" place a call to this function:
public static void setJPanelMenuBar(JPanel parent, JPanel child, JMenuBar menuBar) {
parent.removeAll();
parent.setLayout(new BorderLayout());
JRootPane root = new JRootPane();
parent.add(root, BorderLayout.CENTER);
root.setJMenuBar(menuBar);
root.getContentPane().add(child);
parent.putClientProperty("root", root); //if you need later
}
For example, your ctor might look like this:
public MyPanel() {
initComponents();
setJPanelMenuBar(this, child, myMenuBar);
}
Works for me. Got the idea by looking at JInternalFrame source code. All it does is replace the child JPanel with a JRootPane() and then put the child into the root pane's content pane.

- 1,648
- 1
- 16
- 21
Try putting a jDesktopPane on your panel and then add a menubar to that. I'm using a tabbed pane in my example below, but it should work the same for a panel.
JDesktopPane desktopPane = new JDesktopPane();
tabbedPane.addTab("New tab", null, desktopPane, null);
JMenuBar menuBar_1 = new JMenuBar();
menuBar_1.setBounds(0, 0, 441, 21);
desktopPane.add(menuBar_1);
-
-
I'm building an app where I want to have a menubar in each detachable tab to display multiple World Wind views. This seemed like a good way to do it. Couldn't find any other way to have a separate menubar for each window. – user2248926 Apr 05 '13 at 14:07
I tried too but JMenuItem
with Jmenu
and JmenuBar
was not added to JPanel
.
But you can get that feel if you declare JFrame
's layout as null then use setBounds(x, y, width, height)
on JMenuBar
instance then add the menu bar to JFrame
.

- 15,955
- 11
- 53
- 65