JMenu




The JMenu class holds JMenuItem objects. You must add an import statement:
import javax.swing.JMenu;
JMenu is a component or widget. You add JMenu objects to a JMenuBar (menuBar.add(fileMenu);)

Examples:


JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");


Other Common Methods:




A Sample Program
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;  // File

public class MyClass extends JFrame implements  ActionListener, KeyListener
{ 

  // ***** declaration of JFrame variables *****

  
  // define a mainPanel for components
  JPanel    mainPanel;  

  // ***** declaration of menu variables *****

  // define a menu bar variable to hold JMenus
  JMenuBar  menuBar;
  
  // define some JMenus and their JMenuItems
  // define a JMenu called fileMenu and add menuItems
  JMenu     fileMenu;
  JMenuItem openMenuItem;
  JMenuItem saveMenuItem;
  JMenuItem exitMenuItem;
  
  // define a JMenu called editMenu and add menuItems
  JMenu     editMenu;
  JMenuItem cutMenuItem;
  JMenuItem copyMenuItem;
  JMenuItem pasteMenuItem;
  JMenuItem selectAllMenuItem;
  
 
  // define JPanels for a BorderLayout
  JPanel     northPanel;
  JPanel     southPanel;
  JPanel     westPanel;
  JPanel     eastPanel;
  JPanel     centerPanel;


  // define buttons, labels, etc.  
  JButton   myOkButton;
  JLabel    myTitleLabel;
  JTextArea myTextArea;
  
  
  
  // ***** public void initialize *****

  public void initialize( )
  { 


    
    // create a mainPanel for components
    mainPanel = new JPanel();    

    // ***** assignments for menu variables *****

    menuBar            = new JMenuBar();
    
    fileMenu           = new JMenu("File");
    openMenuItem       = new JMenuItem("Open");
    saveMenuItem       = new JMenuItem("Save");
    exitMenuItem       = new JMenuItem("Exit");
    
    editMenu           = new JMenu("Edit");
    cutMenuItem        = new JMenuItem("cut");
    copyMenuItem       = new JMenuItem("copy");
    pasteMenuItem      = new JMenuItem("paste");
    selectAllMenuItem  = new JMenuItem("Select All");
    
    // add mnemonics to the menu system
    fileMenu.setMnemonic('F');
    exitMenuItem.setMnemonic('x');
    
    // add the menuItem addActionListener(this) calls
    // fileMenu
    openMenuItem.addActionListener(this);
    saveMenuItem.addActionListener(this);
    exitMenuItem.addActionListener(this);
    
    // editMenu
    cutMenuItem.addActionListener(this);
    copyMenuItem.addActionListener(this);
    pasteMenuItem.addActionListener(this);
    selectAllMenuItem.addActionListener(this);
    
    // add menuItems to the fileMenu
    fileMenu.add(openMenuItem);
    fileMenu.add(saveMenuItem);
    fileMenu.addSeparator();
    fileMenu.add(exitMenuItem);
    
    // add menuItems to the editMenu
    editMenu.add(cutMenuItem);
    editMenu.add(copyMenuItem);
    editMenu.add(pasteMenuItem);
    editMenu.add(selectAllMenuItem);
    
    // add menus to the menuBar
    menuBar.add(fileMenu);
    menuBar.add(editMenu);
    
    // attach the JMenuBar to the Window
    setJMenuBar(menuBar);
    
    
    // ***** create JPanels for a BorderLayout *****
    northPanel   = new JPanel();
    southPanel   = new JPanel();
    westPanel    = new JPanel();
    eastPanel    = new JPanel();
    centerPanel  = new JPanel();

    mainPanel.setLayout(new BorderLayout());

    northPanel.setBackground(new Color(115,205,255));
    southPanel.setBackground(new Color(115,205,255));
    westPanel.setBackground(new Color(115,205,255));
    eastPanel.setBackground(new Color(115,205,255));
    
    
    // ***** You need to add buttons, etc. to the 5 panels *****
    myOkButton      = new JButton("Ok");
    myOkButton.addActionListener(this); // this class will handle click event
        
    myTitleLabel    = new JLabel("Hello World");

    myTextArea = new JTextArea("I can type text here.",20,50);
    myTextArea.setFont(new Font("Courier", Font.PLAIN, 16));
    JScrollPane scrollPane = new JScrollPane(myTextArea);
    centerPanel.add(scrollPane);
    
    northPanel.add(myTitleLabel);
    
    southPanel.add(myOkButton);
    

    // ***** add the panels to the mainPanel 5 areas *****
    mainPanel.add(northPanel,BorderLayout.NORTH);
    mainPanel.add(southPanel,BorderLayout.SOUTH);
    mainPanel.add(eastPanel,BorderLayout.EAST);
    mainPanel.add(westPanel,BorderLayout.WEST);
    mainPanel.add(centerPanel,BorderLayout.CENTER);

    // make the mainPanel be the main content area and show it
    setContentPane(mainPanel);
    setVisible(true);  // always show the screen last
  }   // end of public void initialize 

  // ***** default constructor *****

  public MyClass( )
  { 
    // initialize variables

    setSize(800,600);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("Title at top of screen");


    initialize( );

  }

  // ***** ActionListener interface methods *****

  // start of actionPerformed (ActionListener interface)
  public void actionPerformed(ActionEvent e)
  {
    Object source = e.getSource();
    if (source == exitMenuItem)
    {
      System.exit(0);
    }  // end of if
    else if (source == cutMenuItem)
    {
      // do something
    }  // end of if
    else if (source == copyMenuItem)
    {
      // do something
    }  // end of if
    else if (source == pasteMenuItem)
    {
      // do something
    }  // end of if
    else if (source == selectAllMenuItem)
    {
      // do something
    }  // end of if    
    else if (source == myOkButton) // handle button click
    {
      // do something
      JOptionPane.showMessageDialog(this,"You clicked me!");
    }  // end of if
    
    else if (source == openMenuItem)
    {
      // open a file
      JFileChooser myFileChooser = new JFileChooser();

      int choice = myFileChooser.showOpenDialog(this);
      if (choice == JFileChooser.APPROVE_OPTION) 
      {
         // The user selected a file
         File myFile = myFileChooser.getSelectedFile();

         // Open the file and use it
    
      }      
      
    }  // end of if    
    
    else if (source == saveMenuItem)
    {
      // open a file
      JFileChooser myFileChooser = new JFileChooser();

      int choice = myFileChooser.showSaveDialog(this);
      if (choice == JFileChooser.APPROVE_OPTION) 
      {
         // The user selected a file
         File myFile = myFileChooser.getSelectedFile();

         // save the file   myFile.getName(), myFile.getPath()
         
    
      }      
      
    }  // end of if    

        
  }  // end of actionPerformed


  // ***** KeyListener interface methods *****

  // start of keyTyped (KeyListener interface)
  public void keyTyped(KeyEvent e)
  {
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_SPACE)
    {
      // do something;
    }  // end of if
  }  // end of keyTyped(KeyEvent e)

  // start of keyPressed (KeyListener interface)
  public void keyPressed(KeyEvent e)
  {
    // this is an empty method
  }  // end of keyPressed(KeyEvent e)

  // start of keyReleased (KeyListener interface)
  public void keyReleased(KeyEvent e)
  {
    // this is an empty method
  }  // end of keyReleased(KeyEvent e)


  // ***** main method *****
  public static void main(String[] arguments)
  {
    MyClass myClass = new MyClass( );
  }


} // end of class MyClass


Download MyClass.java

  • JTextArea Example