Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Menus will not appear in JMenubar

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Sneaky
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Menus will not appear in JMenubar

    I have created 2 JMenus with various JButtons, JCheckboxes and a submenu. They are compiling and running ok. The problem is that they filling the full length of the JFrame, instead of appearing as two menus in a JMenubar which is what I need.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class MenuExample extends JFrame
    {
    	private JMenuBar menuBar;
    	private JMenu menu, subMenu;
    	private JMenuItem menuItem;
    	private JRadioButtonMenuItem rbMenuItem;
    	private JCheckBoxMenuItem cbMenuItem;
     
    	public MenuExample()                                       						
    	{
    	   menuBar = new JMenuBar();																
     
    		menu = new JMenu("A Menu");															
    		menu.setMnemonic('A');
    		menu.getAccessibleContext().setAccessibleDescription("The only menu in this program that has menu items");
    		menuBar.add(menu);
     
    		menuItem = new JMenuItem("A text-only menu item");
     
    		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
    		menuItem.getAccessibleContext().setAccessibleDescription("This doesn't really do anything");
    		menu.add(menuItem);
     
    		menuItem = new JMenuItem("Both text and icon", new ImageIcon("image/cross.gif"));
    		menuItem.setMnemonic('B');
    		menu.add(menuItem);
     
    		menu.addSeparator();																		
    		ButtonGroup group = new ButtonGroup();
    		rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
    		rbMenuItem.setSelected(true);
    		rbMenuItem.setMnemonic('r');
    		group.add(rbMenuItem);
    		menu.add(rbMenuItem);
     
    		rbMenuItem = new JRadioButtonMenuItem("Another one");
    		rbMenuItem.setMnemonic('o');
    		group.add(rbMenuItem);
    		menu.add(rbMenuItem);
     
    		menu.addSeparator();																		
    		cbMenuItem = new JCheckBoxMenuItem("A check box menu item");
    		cbMenuItem.setMnemonic('c');
    		menu.add(cbMenuItem);  
     
    		cbMenuItem = new JCheckBoxMenuItem("Another one");
    		cbMenuItem.setMnemonic('h');
    		menu.add(cbMenuItem); 
     
    		menu.addSeparator();																		
    		subMenu = new JMenu("A submenu");
    		subMenu.setMnemonic('s');
    		menu.add(subMenu);
     
    		menuItem = new JMenuItem("An item in the submenu");
    		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK));
    		subMenu.add(menuItem);
     
    		menuItem = new JMenuItem("Another item");
    		subMenu.add(menuItem);
     
    		subMenu = new JMenu("Another Menu");												 
    		menuBar.add(menu);
    		menuBar.add(subMenu);
    		add(menuBar);
    	}
     
    	public static void main(String[] args) 
            {
    	   MenuExample frame = new MenuExample();
    	   frame.setTitle("Menu Example");
    	   frame.setSize(300, 250);
    	   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	   frame.setLocationRelativeTo(null);	 
    	   frame.setVisible(true);
             }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Menus will not appear in JMenubar

    Read the API doc for the JFrame class to see how to set its menu bar.
    The add() method is for putting components in the content pane.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Sneaky
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Menus will not appear in JMenubar

    I've tried adding 'frame.setJMenuBar(menuBar);' but it gives me an error. Am I on the right track?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Menus will not appear in JMenubar

    it gives me an error
    Hard to say without seeing the error message.
    Please copy the full text of the error messages and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Sneaky
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Menus will not appear in JMenubar

    MenuExample.java:87: non-static variable menuBar cannot be referenced from a static context
    frame.setJMenuBar(menuBar);
    ^

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Menus will not appear in JMenubar

    Where is line 87? Is it in the main() method? Move the code to the constructor where the variable: menuBar is available.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Sneaky
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Menus will not appear in JMenubar

    Yes line 87 was in the main() method. I moved the code to the constructor, but now I am getting this error:

    MenuExample.java:76: cannot find symbol
    symbol : variable frame
    location: class MenuExample
    frame.setJMenuBar(menuBar);
    ^
    (Line 76 is where I moved the code to the constructor)

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Menus will not appear in JMenubar

    What is supposed to be in the variable frame?
    Where is the frame variable defined?
    What class is the setJMenuBar() method in? Where is there an instance of that class inside of the MenuExample class's constructor?
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    mwardjava92 (March 19th, 2013)

  10. #9
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Sneaky
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Menus will not appear in JMenubar

    I played around with the code and I removed 'frame.' from the setJMenuBar() method and it worked. Thanks man

Similar Threads

  1. What is these menus and How to build them
    By hno2005 in forum Android Development
    Replies: 1
    Last Post: December 1st, 2012, 02:48 AM
  2. JMenuBar disappears half the time when using a certain line of code?
    By !Marcus in forum Java Theory & Questions
    Replies: 2
    Last Post: December 26th, 2011, 04:05 PM
  3. Replies: 33
    Last Post: May 25th, 2011, 06:59 AM
  4. Swing Drop Down Menus
    By x3rubiachica3x in forum AWT / Java Swing
    Replies: 2
    Last Post: November 5th, 2010, 04:58 PM
  5. Menu of Menus Help
    By Strivelli in forum Java Theory & Questions
    Replies: 10
    Last Post: June 5th, 2010, 02:40 PM