JMenu Example Java Swing


JMenu program comprises of menu bar, menus, menu items and separators etc. JMenuItem can be added with images, check boxes, combo boxes etc.

Some objects in this program are declared final and if not they cannot be accessed from inner classes.

Example on JMenu
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;  		                  // for Vector class

public class JMenuDemo extends JFrame 
{
  public JMenuDemo()
  {
    JMenuBar mb = new JMenuBar();
    setJMenuBar(mb);
    final Container c = getContentPane();
                                                  // adding menu items without icons and with icons
    JMenu theatres = new JMenu("Cinema Halls");
    theatres.add("Sudersan 70MM");

    JMenuItem mi1 = new JMenuItem("Imax1");
    JMenuItem mi2 = new JMenuItem("Imax2",new ImageIcon("bird3.gif"));
    theatres.add(mi1);
    theatres.add(mi2);

    JCheckBoxMenuItem mi3 = new JCheckBoxMenuItem("Sandhya");
    JCheckBoxMenuItem mi4 = new JCheckBoxMenuItem("Odean",new ImageIcon("bird2.gif"));
    theatres.add (mi3);
    theatres.add (mi4);

    theatres.addSeparator();

    JRadioButtonMenuItem mi5 = new JRadioButtonMenuItem("Maheswari");
    JRadioButtonMenuItem mi6 = new JRadioButtonMenuItem("Parameswari",new ImageIcon("bird4.gif"));
    theatres.add(mi5);
    theatres.add(mi6);

    mb.add(theatres);
		                                   // adding menu items with icons
    JMenu birds = new JMenu("Birds");
    birds.setMnemonic('B');                        // creating shortcut key

    Icon bird1 = new ImageIcon("bird1.gif","Show the first bird");
    final JMenuItem mi7 = new JMenuItem("First Bird",bird1);

    mi7.setHorizontalTextPosition(SwingConstants.LEFT); // default RIGHT
    mi7.setMnemonic(KeyEvent.VK_F);                // press alt + F
    mi7.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,Event.CTRL_MASK)); // without dropdown list, type ctrl + F

    birds.add(mi7);
    mb.add(birds);
		                                   // adding combo box to the menu bar
    Vector vect = new Vector( );
    vect.add("Red");
    vect.add("Green");
    vect.add("Blue");	
    final JComboBox cbox = new JComboBox(vect);
    cbox.addItemListener(new ItemListener()
    {
      public void itemStateChanged(ItemEvent e)
      {
    	String str = (String) cbox.getSelectedItem();
	System.out.println( str + " is selected");

	if(cbox.getSelectedItem( ).equals("Red"))
      	  c.setBackground(Color.red);
	else if(str.equals("Green"))
      	  c.setBackground(Color.green);
	else if(str.equals("Blue"))
      	  c.setBackground(Color.blue);
      }});
    JMenu alphabets = new JMenu("Aplphabets");
    alphabets.add(cbox);
    mb.add(alphabets);

    setSize(300,300);
    setVisible(true);
  }
  public static void main(String args[] )
  {
    new JMenuDemo();
   }   
}

JMenu

For a similar AWT Menus program refer Menu – Multiple Pull-down Lists.

Leave a Comment

Your email address will not be published.