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:
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.Code:public class MenuBar extends JMenuBar implements ActionListener { }
We are going to add two of them.
Add the above lines just below the class declaration line. Now in the constructor, add these lines:Code:JMenu menuFile = new JMenu("File"); JMenu menuEdit = new JMenu("Edit");
This will make your constructor look like this:Code:add(menuFile); add(menuEdit);
We also need to add four menu items.Code:public MenuBar() { add(menuFile); add(menuEdit); }
itmNew
itmClose
itmUndo
itmCut
itmNew and itmClose will be displayed under the file menu.
Add these four variables to the class variables section:
The first two are for the file menu and the other two are for the edit menu.Code:JMenuItem itmNew = new JMenuItem("New"); JMenuItem itmClose = new JMenuItem("Close"); JMenuItem itmUndo = new JMenuItem("Undo"); JMenuItem itmCut = new JMenuItem("Cut");
Now in our constructor, we will add them to the appropriate menu.
Your constructor should now look like:Code:menuFile.add(itmNew); menuFile.addSeperator(); menuFile.add(itmClose); menuEdit.add(itmUndo); menuEdit.addSeperator(); menuEdit.add(itmCut);
Now, all that is left is to set up the action listeners and action commands.Code:public MenuBar() { menuFile.add(itmNew); menuFile.addSeperator(); menuFile.add(itmClose); menuEdit.add(itmUndo); menuEdit.addSeperator(); menuEdit.add(itmCut); add(menuFile); add(menuEdit); }
The action command is what we will use in the action performed method to determine which menu item was clicked.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);
Add this method:
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.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"); } }
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.
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.Code:public class frame extends JFrame { public frame() { this.setJMenuBar(new MenuBar()); } }
I can't keep up with them. +rep
This is very similar to wxWidgets and gtkmm's approach to the menu bar, as well.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks