In NetBeans how do I add a jMenuBar to a JPanel?

14,598

Solution 1

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

Solution 2

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

Solution 3

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.

Solution 4

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.

Share:
14,598
AdamOutler
Author by

AdamOutler

I'm a Biomed and a Developer.

Updated on June 19, 2022

Comments

  • AdamOutler
    AdamOutler about 2 years

    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.

  • AdamOutler
    AdamOutler over 12 years
    How do I reference that? I'm having problems with getting it to show up in my JFrame? It keeps getting put under "Other Components".
  • Hovercraft Full Of Eels
    Hovercraft Full Of Eels over 12 years
    Sorry, but this is very wrong. If you wanted to add JMenuBar to any component, you are free to do so. -1 vote.
  • Hovercraft Full Of Eels
    Hovercraft Full Of Eels over 12 years
    Please see example code that proves this wrong. Please delete your answer as it is very misleading. I have left a flag for the moderators to delete this answer if you won't.
  • AdamOutler
    AdamOutler over 12 years
    Actually, this is correct. I just extended JFrame instead of JPanel in my form and it now works... however I've got a Stack Overflow error when I exit. Should I start a new topic to track down the Stack Overflow?
  • AdamOutler
    AdamOutler over 12 years
    vextorspace was actually spot-on. However now I'm working with a stack overflow error from converting from a JPanel to a JFrame.
  • Hovercraft Full Of Eels
    Hovercraft Full Of Eels over 12 years
    @AdamOutler: I'm sorry but you're wrong in this. His statement that I quote above is flat out false. If you are telling me that I am wrong, please back this up with code to prove it because I think that I know a little bit more about Swing than you and certainly more than vextorspace. Yes you can solve your problem by having your class extend JFrame, but that's not always the best way. If you showed more of your code, I think I could show you a better way, but the bottom line is vextorspace shouldn't be spreading false information that will mislead you and others.
  • Hauke Ingmar Schmidt
    Hauke Ingmar Schmidt over 12 years
    From the link posted: "In theory, all top-level containers can hold a menu bar. In practice, however, menu bars usually appear only in frames and applets." And why not? JMenuBar is just a JComponent.
  • Hovercraft Full Of Eels
    Hovercraft Full Of Eels over 12 years
    @his: exactly. This is misinformation and the answer should be deleted or in the very least corrected.
  • Hovercraft Full Of Eels
    Hovercraft Full Of Eels over 12 years
    If vextorspace won't take the time or effort to correct his post, I'll do it myself. The answer has been edited so as to not be misleading to newbies.
  • vextorspace
    vextorspace over 12 years
    I was simply going with which components had a setJMenuBar() routine, but I always was annoyed by not being able to add it to a Panel, I will give this a shot!
  • nIcE cOw
    nIcE cOw over 12 years
    @HovercraftFullOfEels : I had edited the answer a bit more, do check and point me if I am wrong in editing this answer a bit further, to me it seems like the actual interpretation of how to add JMenuBars.
  • AdamOutler
    AdamOutler over 12 years
    @hovercraft. Java can do it, netbeans can't. that's all there is to it. I know that you can add components to other components. The problem is doing it in a drag and drop fashion. Rather than jPanel, I switched to jFrame