Results 1 to 6 of 6

Thread: How to use MenuBar class of java?

  1. #1
    Join Date
    Jan 2010
    Posts
    56

    How to use MenuBar class of java?

    Hi All,

    I just started to study the classes of java language. This concepts seems very tough to understand as compare to C++ concept. The 'MenuBar class' is one of these classes. I have referred lots of java book for MenuBar class, but nothing was helpful. If you have good knowledge about the MenuBar class of java, then please let me know that. You help would be greatly appreciable.

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to use MenuBar class of java?

    The MenuBar class encapsulates the platform's concept of a menu bar bound to a frame. In order to associate the menu bar with a Frame object, call the frame's setMenuBar method. A menu bar handles keyboard shortcuts for menu items, passing them along to its child menus. (Keyboard shortcuts, which are optional, provide the user with an alternative to the mouse for invoking a menu item and the action that is associated with it.)

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    Re: How to use MenuBar class of java?

    Following example will depict you the use of MenuBar class:
    Code:
    java.awt.MenuBar
    
    public class MenuBarDM extends JFrame {
      JMenuBar menubar1 = new JMenuBar();
      ImageIcon icon1 = new ImageIcon("exit.png");
    
      JMenu file1 = new JMenu("File");
    
      public MenuMy1() {
        file1.setMnemonic(KeyEvent.VK_F);
    
        JMenuItem fileClose1 = new JMenuItem("Close", icon);
        fileClose1.setMnemonic(KeyEvent.VK_C);
        fileClose1.setToolTipText("Exit application");
        fileClose1.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            System.exit(0);
          }
        };
        file1.add(fileClose1);
        menubar.add(file1);
        setJMenuBar(menubar1);
    
        setSize(350, 200);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
      }
      public static void main(String[] armgs)
     {
        new MenuMy1();
      }
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to use MenuBar class of java?

    Hi friend,

    I suggest you to study following some important methods of MenuBar class:
    • setHelpMenu(Menu mnu)
    • remove(MenuComponent mnComp)
    • remove(int index)
    • getShortcutMenuItem(MenuShortcut mnSht)
    • deleteShortcut(MenuShortcut mnSht)

  5. #5
    Join Date
    May 2008
    Posts
    2,012

    Re: How to use MenuBar class of java?

    Hi,

    If you want to know how to create Vertical MenuBar using MenuBar class, then please refer below example:
    Code:
    import java.awt.GridLayout;
    import java.awt.LayoutManager;
    java.awt.MenuBar;
    
    class VerticalMenuBarDM
    {
      private static final LayoutManager grid = new GridLayout(0,1);
      public VerticalMenuBar() {
        setLayout(grid);
      }
    }
    
    public class VerticalMenuBarBT {
    
      public static void main(final String args[]) {
        JFrame zcf = new JFrame("MenuSample Example");
        zcf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JMenuBar menuBar = new VerticalMenuBar();
        JMenu fileMenuD = new JMenu("File");
        fileMenu.setMnemonic(KeyEvent.VK_F);
        menuBar.add(fileMenuD);
        JMenu editMenu1 = new JMenu("Edit");
        menuBar.add(editMenu);
        zcf.setJMenuBar(menuBar);
        zcf.setSize(300, 250);
        zcf.setVisible(true);
      }
    }

  6. #6
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to use MenuBar class of java?

    The MenuBar class basically implements MenuContainer, Accessible, Serializable. This java class extends MenuComponent. There is only one nested class available for MenuBar class and which is 'MenuBar.AccessibleAWTMenuBar'. You can use only single constructor of MenuBar class. This constructor doesn't contains any input parameter i.e it is a empty constructor.

Similar Threads

  1. Converting java class to java bean class
    By Ucchal in forum Software Development
    Replies: 6
    Last Post: 09-08-2010, 10:24 AM
  2. MenuItem class of java
    By Zecho in forum Software Development
    Replies: 4
    Last Post: 05-03-2010, 08:30 PM
  3. How to use Event class of java?
    By Protectors in forum Software Development
    Replies: 5
    Last Post: 25-02-2010, 08:15 AM
  4. DebugGraphics class of java
    By Prashobh Mallu in forum Software Development
    Replies: 5
    Last Post: 19-02-2010, 02:43 PM
  5. Java Programming Language Basics: Reflection Basics and Class Class
    By mayuri_gunjan in forum Guides & Tutorials
    Replies: 6
    Last Post: 29-08-2005, 12:04 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,223,335.65859 seconds with 17 queries