JaVa
   

The JMenu

It is possible to create menus that appear at the top of your app. This can be very useful if you wish to create a tool for your game, such as a map editor. We are only using menus at a basic level in this tutorial, but they can be quite complex if you experiment further with them. Let's now look at a simple menu example:

Code Listing 20: Using the JMenu
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JMenuExample extends JFrame implements ActionListener
{
 public static void main(String[] argv)
 {
 JMenuExample mainApp = new JMenuExample();
 }
 public JMenuExample()
 {
 super("JMenu Example");
 setBounds(0, 0, 300, 300);
 getContentPane().setLayout(null);
 setDefaultCloseOperation(EXIT_ON_CLOSE);
 menuBar = new JMenuBar();
 menuBar.setBounds(0, 0, 300, 25);
 // Create the file menu...
 fileMenu = new JMenu("File");
 fileMenu.setMnemonic('F');
 openItem = new JMenuItem("Open...");
 openItem.addActionListener(this);
 saveItem = new JMenuItem("Save...");
 saveItem.addActionListener(this);
 quitItem = new JMenuItem("Quit");
 quitItem.addActionListener(this);
 // Add the three items to the fileMenu object...
 fileMenu.add(openItem);
 fileMenu.add(saveItem);
 fileMenu.addSeparator();
 fileMenu.add(quitItem);
 // Create the color menu...
 colorMenu = new JMenu("Color");
 colorMenu.setMnemonic('C');
 redItem = new JMenuItem("Red Background");
 redItem.addActionListener(this);
 greenItem = new JMenuItem("Green Background");
 greenItem.addActionListener(this);
 blueItem = new JMenuItem("Blue Background");
 blueItem.addActionListener(this);
 // Add the three items to the colorMenu object...
 colorMenu.add(redItem);
 colorMenu.add(greenItem);
 colorMenu.add(blueItem);
 // Add both the menus to the main menu bar...
 menuBar.add(fileMenu);
 menuBar.add(colorMenu);
 getContentPane().add(menuBar);
 setVisible(true);
 }
 public void actionPerformed(ActionEvent e)
 {
 if(e.getSource() == openItem)
 {
 JOptionPane.showMessageDialog(this, "You selected the
 'open' menu option", "Info", JOptionPane.INFORMATION_MESSAGE); }
 else if(e.getSource() == saveItem)
 {
 JOptionPane.showMessageDialog(this, "You selected the
 'save' menu option", "Info",
 JOptionPane.INFORMATION_MESSAGE); }
 else if(e.getSource() == quitItem)
 {
 System.exit(1); }
 else if(e.getSource() == redItem)
 {
 getContentPane().setBackground(new Color(255, 0, 0));
 }
 else if(e.getSource() == greenItem)
 {
 getContentPane().setBackground(new Color(0, 255, 0));
 }
 else if(e.getSource() == blueItem)
 {
 getContentPane().setBackground(new Color(0, 0, 255));
 }
 }
 JMenuBar menuBar;
 JMenu fileMenu;
 JMenuItem openItem;
 JMenuItem saveItem;
 JMenuItem quitItem;
 JMenu colorMenu;
 JMenuItem redItem;
 JMenuItem greenItem;
 JMenuItem blueItem;
}


Java End example

When we execute the menu example and expand the File menu, it will look like the following:

Java ScreenShot
Screenshot: Using a menu

Let's now look at the code that we used to create this. Our base menu is created by using the JMenuBar class. We simply call the constructor with no parameters to create an instance of this, as follows:

menuBar = new JMenuBar();


Following that, we set the bounds for the menu, which in our case is along the top of the app. After setting the bounds, we then create an object called fileMenu, which refers to a JMenu class. Once we create the object, we then set the mnemonic to be F, meaning that if the user presses the Alt+F combination on the keyboard, the menu will open. The code segment required to do this can be seen here:

fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');


Once we have our JMenu object, we can then create JMenuItem objects to add to our JMenu object. Each JMenuItem object we add to our JMenu object will appear below the previous one when the user drops the menu down. Let's look now at the code segment that we used to create the complete File menu:

openItem = new JMenuItem("Open...");
openItem.addActionListener(this);
saveItem = new JMenuItem("Save...");
saveItem.addActionListener(this);
quitItem = new JMenuItem("Quit");
quitItem.addActionListener(this);
// Add the three items to the fileMenu object...
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.addSeparator();
fileMenu.add(quitItem);


First we created three JMenuItem objects (openItem, saveItem, and quitItem), passing the text that we wish to appear on the menu item into the JMenuItem constructor. Also note that we add an ActionListener to each of the menu items. This is used to determine what code to execute when the menu option is selected by the user, as we will see later. Once our JMenuItem objects are created, we then simply call the add method of our fileMenu object to add them to the menu. Note how we have used the addSeparator method to add a horizontal separator bar. Next we create our Color menu in exactly the same way. The code for this is as follows:

// Create the color menu...
colorMenu = new JMenu("Color");
colorMenu.setMnemonic('C');
redItem = new JMenuItem("Red Background");
redItem.addActionListener(this);
greenItem = new JMenuItem("Green Background");
greenItem.addActionListener(this);
blueItem = new JMenuItem("Blue Background");
blueItem.addActionListener(this);
// Add the three items to the colorMenu object...
colorMenu.add(redItem);
colorMenu.add(greenItem);
colorMenu.add(blueItem);


Notice for the Color menu we set the mnemonic to be C, which means that the menu can be opened by pressing the key combination Alt+C. After both our menus are constructed, we can then add them to our JMenuBar, simply using the add method again. This can be seen in the following two lines of code:

menuBar.add(fileMenu);
menuBar.add(colorMenu);


Finally, we add the menuBar object to the content pane with the following line of code:

getContentPane().add(menuBar);


Note in the actionPerformed method how we react to menu options being selected. It is done in exactly the same way as the JButtons. For example, if we wish to check if saveItem has been selected, we use the following if statement:

if(e.getSource() == saveItem)
{
 … } 


JaVa
   
Comments