Thursday, November 11, 2010

Write an application which will create a popup menu with different menuitems and set the label according to menutem

/* Program : 6 
   Write an application which will create a popup menu with different menuitems and set the label according to menutem
   Developed by : Malhar Vora
   Developed on : 8-11-2010
   Development Status : Completed and tested
   Email     : vbmade2000@gmail.com
   WebSite   : www.malhar2010.blogspot.com
*******************************************************************************************************************/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class P6 extends JFrame implements ActionListener
{


 JPopupMenu mnuContext = null;
 JMenuItem mnu1 = null;
 JMenuItem mnu2 = null;
 JMenuItem mnu3 = null;
 JMenuItem mnu4 = null;
 
 P6()
 {
  
  setTitle("Program 6 - Developed by Malhar Vora");
  setSize(400,300);
  setDefaultCloseOperation(EXIT_ON_CLOSE);
  setVisible(true);
  
  mnu1 = new JMenuItem("Abc");
  mnu2 = new JMenuItem("bbc");
  mnu3 = new JMenuItem("pqr");
  mnu4 = new JMenuItem("xyz");
  
  mnuContext = new JPopupMenu();
  mnuContext.add(mnu1);
  mnuContext.add(mnu2);
  mnuContext.add(mnu3);
  mnuContext.add(mnu4);
  
  PopupListener pl = new PopupListener();
  addMouseListener(pl);
  
  mnu1.addActionListener(this);
  mnu2.addActionListener(this);
  mnu3.addActionListener(this);
  mnu4.addActionListener(this);
   
 }
 
 
 public void actionPerformed(ActionEvent ae)
 {
  String cmd = ae.getActionCommand();
  setTitle(cmd);
 }
 
 class PopupListener extends MouseAdapter
 {
  public void mousePressed(MouseEvent me)
  {
   if (me.isPopupTrigger())
   { 
    mnuContext.show(me.getComponent(), me.getX(), me.getY()); 
   } 
  }
 
  public void mouseReleased(MouseEvent me)
  {
   if (me.isPopupTrigger())
   { 
    mnuContext.show(me.getComponent(), me.getX(), me.getY()); 
   } 
  }
}
 
 public static void main(String []str)
 {
  P6 p = new P6();
 }

}




No comments:

Post a Comment