Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add JMenuBar to a JPanel?

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

like image 596
Skizit Avatar asked Nov 28 '10 23:11

Skizit


People also ask

Can you pack a JPanel?

Then the answer to your question is no, there is no other way to do this, the API was designed from the start to make use of the layout managers.

What is JMenuBar in Java?

JMenuBar is an implementation of menu bar . the JMenuBar contains one or more JMenu objects, when the JMenu objects are selected they display a popup showing one or more JMenuItems . JMenu basically represents a menu . It contains several JMenuItem Object . It may also contain JMenu Objects (or submenu).

How do I change the content of a JPanel?

Just call the method pack() after setting the ContentPane , ( java 1.7 , maybe older) like this: JFrame frame = new JFrame(); JPanel panel1 = new JPanel(); JPanel panel2 = new JPanel(); .... frame. setContentPane(panel1); frame. pack(); ... frame.


1 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.

like image 55
Reboot Avatar answered Oct 14 '22 09:10

Reboot