In NetBeans, how to add jMenuBar to JPanel?

I have problems and I don’t understand why. I have JFrame and JPanel and everything works correctly. I am trying to add jMenuBar to JPanel and I cannot make it appear. It is placed under "Other components" and is not displayed at run time. any suggestions?

edit: It seems that the corresponding NetBeans answer cannot add JMenu to the JFrame. I wanted to add this to the first post because the corresponding answer below was voted.

+6
source share
4 answers

One of the smart ways is to double-click on the JFrame, which is located on the project panel, it appears in a new window with the actual JFrame in the left panel of the palette, all swing components appear; you just need to drag the item into this frame the code will be automatically generated nb you can also add an event to this element by right-clicking on it

+6
source

JMenuBar is added to the JFrame using the setJMenuBar (...) method.

A little code to help you:

import javax.swing.*; public class MenuBarTest extends JFrame { public MenuBarTest() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); JPanel contentPane = new JPanel(); contentPane.setBackground(java.awt.Color.WHITE); JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu("File"); JMenuItem menuItem = new JMenuItem("Open"); menu.add(menuItem); menuBar.add(menu); setContentPane(contentPane); setJMenuBar(menuBar); setSize(200, 200); setVisible(true); } public static void main(String... args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new MenuBarTest(); } }); } } 
+8
source

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)); // rigid area just as a place-holder mainPanel.add(Box.createRigidArea(new Dimension(400, 150))); mainPanel.add(menuBarHoldingPanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(mainPanel); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } } 

Not only is this easy to do, there are many cases where this is desired.

+5
source

Since JMenuBar comes from JComponent, it can be added to any container (usually using BorderLayout to position BorderLayout.PAGE_START), it is most often added to JApplet, JDialog, JFrame, JInternalFrame, JRootPane via setJMenuBar (...) .

http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html

Just a small addition:

The menu bar contains one or more menus and has the usual platform-specific location β€” usually at the top of the window.

+4
source

Source: https://habr.com/ru/post/908790/


All Articles