6

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
  • 870
  • 4
  • 13
  • 29
  • Possible duplicate of [add JMenuBar to a JPanel?](http://stackoverflow.com/questions/4299846/add-jmenubar-to-a-jpanel) – ryvantage Oct 26 '15 at 20:59

4 Answers4

8

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();
            }
        });
    }
}
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
6

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

krupal
  • 403
  • 4
  • 15
5

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • vextorspace was actually spot-on. However now I'm working with a stack overflow error from converting from a JPanel to a JFrame. – AdamOutler Feb 18 '12 at 19:04
  • @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. – Hovercraft Full Of Eels Feb 18 '12 at 21:54
  • 1
    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! – vextorspace Feb 19 '12 at 14:22
  • @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 – AdamOutler Feb 27 '12 at 06:30
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.

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
vextorspace
  • 934
  • 2
  • 10
  • 25
  • 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". – AdamOutler Feb 18 '12 at 18:14
  • 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 Feb 18 '12 at 18:15
  • 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. – Hovercraft Full Of Eels Feb 18 '12 at 18:22
  • 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 Feb 18 '12 at 19:03
  • 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`. – Hauke Ingmar Schmidt Feb 18 '12 at 21:58
  • @his: exactly. This is misinformation and the answer should be deleted or in the very least corrected. – Hovercraft Full Of Eels Feb 18 '12 at 22:13
  • 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. – Hovercraft Full Of Eels Feb 18 '12 at 23:09
  • 1
    @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. – nIcE cOw Feb 20 '12 at 07:25