+ Reply to Thread
Results 1 to 3 of 3

Thread: JMenuBar

  1. #1
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    JMenuBar

    JMenu in Swing

    Windows users are used to a certain interface, and it is best if you keep your program has similar to the interface as possible so the user doesn't have to learn an entirely new interface (when possible).

    A menu is something that appears at the top of almost every program you will use. It contains short cuts to commonly use features of the program.

    In this tutorial we are going to create a simple menu that has two menus menu items and other each menu item, we will have 2 menu items. They will be separated by a separator. When you click on each menu item, a popup message will be displayed.

    We are going to create our class as an extension of the JMenuBar so that we can create an extension of the JFrame that adds the menu bar to every form.

    First the class skeleton:

    Code:
    public class MenuBar extends JMenuBar implements ActionListener {
    
    }
    The action listener is required for us to listen for click events on each menu item. Each item in the menu is a JMenu object.

    We are going to add two of them.

    Code:
    JMenu menuFile = new JMenu("File");
    JMenu menuEdit = new JMenu("Edit");
    Add the above lines just below the class declaration line. Now in the constructor, add these lines:

    Code:
    add(menuFile);
    add(menuEdit);
    This will make your constructor look like this:

    Code:
    public MenuBar() {
    	add(menuFile);
    	add(menuEdit);
    }
    We also need to add four menu items.

    itmNew
    itmClose
    itmUndo
    itmCut

    itmNew and itmClose will be displayed under the file menu.

    Add these four variables to the class variables section:

    Code:
    JMenuItem itmNew = new JMenuItem("New");
    JMenuItem itmClose = new JMenuItem("Close");
    JMenuItem itmUndo = new JMenuItem("Undo");
    JMenuItem itmCut = new JMenuItem("Cut");
    The first two are for the file menu and the other two are for the edit menu.

    Now in our constructor, we will add them to the appropriate menu.

    Code:
    menuFile.add(itmNew);
    menuFile.addSeperator();
    menuFile.add(itmClose);
    
    menuEdit.add(itmUndo);
    menuEdit.addSeperator();
    menuEdit.add(itmCut);
    Your constructor should now look like:

    Code:
    public MenuBar() {
    	menuFile.add(itmNew);
    	menuFile.addSeperator();
    	menuFile.add(itmClose);
    
    	menuEdit.add(itmUndo);
    	menuEdit.addSeperator();
    	menuEdit.add(itmCut);
    
    	add(menuFile);
    	add(menuEdit);
    }
    Now, all that is left is to set up the action listeners and action commands.

    Code:
    itmNew.setActionCommand("new");
    itmNew.addActionListener(this);
    itmClose.setActionCommand("close");
    itmClose.addActionListener(this);
    itmUndo.setActionCommand("undo");
    itmUndo.addActionListener(this);
    itmCut.setActionCommand("cut");
    itmCut.addActionListener(this);
    The action command is what we will use in the action performed method to determine which menu item was clicked.

    Add this method:

    Code:
    public void actionPerformed(ActionEvent e) {
    	if (e.getActionCommand().equals("new")) {
    		JOptionPane.showMessageDialog(null,"New");
    	} else if (e.getActionCommand().equals("close")) {
    		JOptionPane.showMessageDialog(null,"Close");
    	} else if (e.getActionCommand().equals("undo")) {
    		JOptionPane.showMessageDialog(null,"Undo");
    	} else if (e.getActionCommand().equals("cut")) {	
    		JOptionPane.showMessageDialog(null,"Cut");
    	}
    }
    The getActionCommand will return the action command associated with the menu item that was clicked. This we can use to determine what action to perform.

    Now, the last thing we are going to do is create a JFrame class that will have the menubar added to it as soon as we create it declared.

    Code:
    public class frame extends JFrame {
    	public frame() {
    		this.setJMenuBar(new MenuBar());
    	}
    }
    Now instead of creating an instance of JFrame you will create an instance of frame and it will always have the menu bar on the frame.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: JMenuBar

    I can't keep up with them. +rep

  4. #3
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: JMenuBar

    This is very similar to wxWidgets and gtkmm's approach to the menu bar, as well.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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