add JMenuBar to a JPanel?

Skizit picture Skizit · Nov 29, 2010 · Viewed 39.1k times · Source

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

Answer

Reboot picture Reboot · Nov 29, 2010

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.