In NetBeans how do I add a jMenuBar to a JPanel?
Asked Answered
G

4

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.

Gunnysack answered 18/2, 2012 at 18:5 Comment(1)
Possible duplicate of add JMenuBar to a JPanel?Auguste
S
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

Scalp answered 19/2, 2012 at 11:45 Comment(0)
B
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();
            }
        });
    }
}
Blader answered 18/2, 2012 at 18:8 Comment(0)
S
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

Scalp answered 19/2, 2012 at 11:45 Comment(0)
X
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.

Xylophagous answered 18/2, 2012 at 18:22 Comment(4)
vextorspace was actually spot-on. However now I'm working with a stack overflow error from converting from a JPanel to a JFrame.Gunnysack
@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.Xylophagous
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!Starvation
@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 jFrameGunnysack
S
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.

Starvation answered 18/2, 2012 at 18:12 Comment(8)
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".Gunnysack
Sorry, but this is very wrong. If you wanted to add JMenuBar to any component, you are free to do so. -1 vote.Xylophagous
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.Xylophagous
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?Gunnysack
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.Saez
@his: exactly. This is misinformation and the answer should be deleted or in the very least corrected.Xylophagous
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.Xylophagous
@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.Blader

© 2022 - 2024 — McMap. All rights reserved.