add JMenuBar to a JPanel?
Asked Answered
L

5

16

I've got a JMenuBar and a JPanel. I'd like to add the JMenuBar to the JPanel. How would I do so?

Linus answered 28/11, 2010 at 23:51 Comment(0)
B
19

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.

Bodega answered 29/11, 2010 at 0:11 Comment(4)
I thought you can only add JMenuBar's to JFrame'sSubstituent
Works, won't allow me to add it to the very top but close enough!Linus
BorderLayout.PAGE_START seemed to put it at the top for me.Goth
Is this code outdated? This does not work any more.Prepotency
A
5

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

Ansell answered 28/11, 2010 at 23:55 Comment(2)
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?Linus
Well, 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!)Ansell
B
1

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.

Burne answered 24/3, 2015 at 0:19 Comment(1)
What's the advantage of using a JRootPane?Trivandrum
K
0

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.

Kantianism answered 10/3, 2011 at 13:19 Comment(0)
P
0

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);
Preachy answered 5/4, 2013 at 11:58 Comment(2)
hmm ... why a JDesktopPane?Purify
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.Preachy

© 2022 - 2024 — McMap. All rights reserved.