Question

I am having problems and I don't really understand why. I have a JFrame and a JPanel and everything works properly. I am trying to add a jMenuBar into the JPanel and I cannot get it to show up. It is being placed under "Other Components" and does not show up during runtime. any suggestions?

edit: It seems that the appropriate answer is NetBeans cannot add a JMenu to a JFrame. I wanted to add this to the first post because the appropriate answer below was down-voted.

Was it helpful?

Solution

One of the smart way is double click on JFrame which is at project bar this appears new window with actual JFrame at the left side palette bar appears there is all component of the swing you have to only drag and drop item to this Frame the code will automatically made by nb you can also add an event to that item by right click on it

OTHER TIPS

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

Small code to help your cause :

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();
            }
        });
    }
}

For vextorspace who states:

JMenuBar can only be added to JFrames, JDialogs, and JApplets.

This example shows that it is easy to add JMenuBar to a JPanel (or any container for that matter):

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

Since a JMenuBar derives from JComponent it can be added to any container (usually one using BorderLayout to the BorderLayout.PAGE_START position), it is most commonly added to JApplet, JDialog, JFrame, JInternalFrame, JRootPane via the setJMenuBar(...) method.

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

Just a small addition :

A menu bar contains one or more menus and has a customary, platform-dependent location — usually along the top of a window.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top