Action Listener

                                        Action Listener in Java

Hello all!🙋

Welcome to my blog lets see how we  can add action listener in menu items in java.We can divide this task into 2 categories that is:-   

        1.How to create JMenubar in java.

        2.How to add action listener .



1.JMenuBar : 

JMenuBar contains one or more JMenu objects, when the JMenu objects are selected they display a popup showing one or more JMenuItems and we can also create subitems in menuitems.Following are the steps to create an JMenubar:-

Step1: Create a menubar by writing 

           JMenuBar mb=new JMenuBar();   ( here mb is an object of menubar)


Step2: Similarly create menu.

           JMenu menu=new JMenu("Menu")


Step3: Now create menu items which will display when menu is clicked.

             JMenuItem i1, i2, i3, i4, i5;

             i1=new JMenuItem("File"); 
             i2=new JMenuItem("Save"); 
             i3=new JMenuItem("Save as"); 
             i4=new JMenuItem("Tools"); 
             i5=new JMenuItem("Exit");


Step4:Now add these items to menu and then add menu to menubar as shown below:

           menu.add(i1); 

           menu.add(i2);
           menu.add(i3);
           menu.add(i4);

           menu.add(i5);

          // now add menu to menubar

          mb.add(menu);    //mb is an object of menubar and we here add menu to it.

Step5:At the end add menubar to frame by the following way:
           f.setJMenuBar(mb); // here ' f ' is object of frame.




2.Adding action listener: 


It is notified against ActionEvent means whenever some event happens like clicking a button or menuitem etc.. The ActionListener interface is found in java.awt.event package. It has only one method: actionPerformed().Following are the steps to add actionlistener :


Step1:Implement the ActionListener interface in the class:
         Implements ActionListener


Step2:Register the component with the Listener:
          component name.addActionListener(this); 

Step3: Override the actionPerformed() method:
          public void actionPerformed(ActionEvent e){ 
           //Write the code here  
          }  


// write a program to add actionlistener in menuitem


import javax.swing.*;
import java.awt.event.*;
public class menubar implements ActionListener {
     JMenu menu, submenu;
     JTextField tf1;
          JMenuItem i1, i2, i3, i4, i5; 
          menubar(){ 
          JFrame f= new JFrame("Menu"); 
          JMenuBar mb=new JMenuBar();
          tf1=new JTextField("Nothing is Selected.");
          tf1.setBounds(100,100,200,200);
          menu=new JMenu("Menu"); 
          submenu=new JMenu("Sub Menu"); 
          i1=new JMenuItem("Item 1"); 
          i2=new JMenuItem("Item 2"); 
          i3=new JMenuItem("Item 3"); 
          i4=new JMenuItem("Item 4"); 
          i5=new JMenuItem("Item 5");
          i1.addActionListener(this);
          i2.addActionListener(this);
          menu.add(i1); menu.add(i2); menu.add(i3); 
          submenu.add(i4); 
          submenu.add(i5); 
          menu.add(submenu); 
          mb.add(menu); 
          f.add(tf1);
          f.setJMenuBar(mb); 
          f.setSize(400,400); 
          f.setLayout(null); 
          f.setVisible(true); 
        } 
      
        public static void main(String args[]) 
          { 
             new menubar(); 
           }
    @Override
    public void actionPerformed(ActionEvent e) {
       tf1.setText("Menuitem 1 is Selected.");
    }
  
}


Output :-
                                                    
when nothing is selected.











See the next post to learn about adjustment listener in java.        



                                               
                                                                                                          
                                                       














Comments

Post a Comment