Drop down Menu

Menus are an integral part of GUI(Graphical User Interface). Menus allow the user to perform actions  with extra GUI components. In Swing GUIs, menus can only be attached to objects of the classes that provide method setJMenuBar.

 

JMenuBar Class----subclass of JComponent(contains the methods necessary to manage a menu bar.

JMenuItem----subclass of javax.swing.AbstractButton(contains the methods necessary to mange menu items.

JMenu Class---subclass of javax.swing.JMenuItem(contains the methods necessary for managing menus.

JCheckBoxMenuItem Class---subclass of javax.swing.JMenuItem(contains the methods necessary to mange menu items that can be toggled on or off.

JRadioButtonMenuItem Class---subclass of javax.swing.JMenuItem(When multiple JRadioButtonMenu Items are maintained as part of a ButtonGroup, one item in the group can be selected.

//MenuTest.java

//Demonstrating menus

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

public class MenuTest extends JFrame

 {

       private Color colorValuess[]=

       {Color.gray, Color.black, Color.blue, Color.green};

      private JRadioButtonMenuItem colorItemss[], fontss[];

      private JCheckBoxMenuItem styleItemss[];

      private JLabel displayy;

     private ButtonGroup fontGroupp, colorGroupp;

     private int stylee;

   

     public MenuTest()

      {

          super(" JMenus in Java Swing");

         JMenuBar barr=new JMenuBar();

         setJMenuBar(barr);

         JMenu fileMenuu=new JMenu("File") ;

         fileMenuu.setMnemonic('F');

        JMenuItem aboutItemm=new JMenuItem("ABOUT JMENU ");

        aboutItemm.setMnemonic('A');

       aboutItemm.addActionListener(

       new ActionListener()  

      {

           public void actionPerformed(ActionEvent eaa)

              {

                   JOptionPane.showMessageDialog(MenuTest.this,

                   "This is an example\n of using menus",

                "About",JOptionPane.PLAIN_MESSAGE);

                }

    }

);

   fileMenuu.add(aboutItemm);

   JMenuItem exitItemm=new JMenuItem("Exit");

   exitItemm.setMnemonic('x');

   exitItemm.addActionListener(

    new ActionListener()

      {

            public void actionPerformed(ActionEvent eaa)

              {

                   System.exit(0);

             }

     }

);

        fileMenuu.add(exitItemm);

        barr.add(fileMenuu);

//create the Format menu, its submenus and menu items

         JMenu formatMenuu=new JMenu("Format");

         formatMenuu.setMnemonic('r');

//create color submenus

       String colorss[]=

                  {"Gray","Black","Blue","Green"};

       JMenu colorMenuu=new JMenu("Color");

       colorMenuu.setMnemonic('c');

       colorItemss=new JRadioButtonMenuItem[colorss.length];

        colorGroupp=new ButtonGroup();

        ItemHandlerr itemHandlerr=new ItemHandlerr();

         for(int ii=0;ii<colorss.length;ii++)

           {

                colorItemss[ii]=

                    new JRadioButtonMenuItem(colorss[ii]);

                colorMenuu.add(colorItemss[ii]);

                colorGroupp.add(colorItemss[ii]);

                colorItemss[ii].addActionListener(itemHandlerr);

          }

               

        colorItemss[0].setSelected(true);

        formatMenuu.add(colorMenuu);

        formatMenuu.addSeparator();

//create font submenus

        String fontNamess[]=

            {"TimesRoman","Courier","Arial"};

       JMenu fontMenuu =new JMenu("font");

      fontMenuu.setMnemonic('n');

      fontss=new JRadioButtonMenuItem[fontNamess.length];

      fontGroupp=new ButtonGroup();

      for(int ii=0;ii<fontss.length;ii++)

       {

                fontss[ii]=

                   new JRadioButtonMenuItem(fontNamess[ii]);

                fontMenuu.add(fontss[ii]);

                fontGroupp.add(fontss[ii]);

                fontss[ii].addActionListener(itemHandlerr);

       }

        fontss[0].setSelected(true);

        fontMenuu.addSeparator();

       String styleNamess[]={"Bold","Italics"};

       styleItemss=new JCheckBoxMenuItem[styleNamess.length];

      StyleHandlerr styleHandlerr=new StyleHandlerr();

     for(int ii=0;ii<styleNamess.length;ii++)

       {

                styleItemss[ii]=

                    new JCheckBoxMenuItem(styleNamess[ii]);

                  fontMenuu.add(styleItemss[ii]);

                  styleItemss[ii].addItemListener(styleHandlerr);

       }

         formatMenuu.add(fontMenuu);

         barr.add(formatMenuu);

         displayy=new JLabel(

          "Sample Text",SwingConstants.CENTER);

          displayy.setForeground(colorValuess[0]);

           displayy.setFont(

           new Font("TimesRoman",Font.PLAIN,70));

  

           getContentPane().setBackground(Color.cyan);

          getContentPane().add(displayy,BorderLayout.CENTER);

  

           setSize(500,300);

          show();

       }

public static void main(String aa[])

    {

                MenuTest appp=new MenuTest();

               

                appp.addWindowListener(

                new WindowAdapter(){

                                public void windowClosing(WindowEvent eww)

                                {

                                                System.exit(0);

                                }

                }

                );

}

               

class ItemHandlerr implements ActionListener

        {

                public void actionPerformed(ActionEvent eaa)

                {

                for(int ii=0;ii<colorItemss.length;ii++)    

                                if(colorItemss[ii].isSelected())

                                     {

                                             displayy.setForeground(colorValuess[ii]);

                                                break;

                                     }

                               

                for(int ii=0;ii<fontss.length;ii++)

                                                if(eaa.getSource()==fontss[ii])

                                                     {

                                                             displayy.setFont(new Font(

                                                                fontss[ii].getText(),stylee,72));

                                                                break;

                                                     }

                                               

                                                repaint();

                }

}

  

   class StyleHandlerr implements ItemListener

                {

                      public void itemStateChanged(ItemEvent eii)

                          {

                                   stylee=0;

                                   if(styleItemss[0].isSelected())   

                                    stylee+=Font.BOLD;

                                    if(styleItemss[1].isSelected())

                                    stylee+=Font.ITALIC;

                                   displayy.setFont(new Font(

                                  displayy.getFont().getName(),stylee,72));

                                        repaint();

                            }

                 }

}

Output........

DropDown Menu

  

                

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @