Hash Table for maintaining popup menus in Java

This section shows the procedure of maintaining popup menus through hash table in your Java program.

Hash Table for maintaining popup menus in Java

This section shows the procedure of maintaining popup menus through hash table in your Java program.

 Hash Table for maintaining popup menus in Java

Hash Table for maintaining popup menus in Java

     

This section shows the procedure of maintaining popup menus through hash table in your Java program. This section provides you an example with the complete code of the program that has been used to create and maintain popup menus in Java. All the elements listed in the hash table are shown in the popup list as per requirement when you right click on the component available on the main frame. But if you perform single click on the component then other type of operation is performed.

Program Description:

Following program maintains the popup menus by storing these names of the component for each and every component available on the main frame or the window of your java application. When you right click on any component then the source of the clicked component is sent for searching in the mentioned hash table and the program searches all similar component's name of the clicked component, and add all name to the popup menu list. But when you click on any item shown in the popup list then the specific operation is performed, which is mentioned for the specific component of the main frame.

Here is the code of the program:

import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.util.*;
import javax.swing.*;

public class HashTableForPopupMenu extends JFrame 
implements ActionListener, MouseListener{
  public static void main(String argv[]){
  new HashTableForPopupMenu().setVisible(true);
  }

  public HashTableForPopupMenu(){
  MenuBar mb = new MenuBar();
  setMenuBar(mb);
  Menu m = new Menu("File");
  mb.add(m);
  MenuItem item = new MenuItem("First Menu");
  item.addActionListener(this);
  m.add(item);
  item = new MenuItem("Second Menu");
  m.add(item);
  item.addActionListener(this);
  setSize(400400);
  setLayout(new BorderLayout());
  Label l = new Label("label");
  addPopup(l, "label");
  add(l, "North");
  Panel p = new Panel();
  addPopup(p, "Panel");
  add(p, "Center");
  Button b = new Button("button");
  b.addActionListener(this);
  addPopup(b, "button");
  add(b, "South");
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public void actionPerformed(ActionEvent e){
  JOptionPane.showMessageDialog
(
null,"This simple Action Event.\nSource=" + e.getSource());
  }

  public void mouseClicked(MouseEvent e){
  mouseAction("mouseClicked", e);
  }

  public void mouseEntered(MouseEvent e){}
  public void mouseExited(MouseEvent e){}
  
  public void mousePressed(MouseEvent e){
  mouseAction("mousePressed", e);
  }

  public void mouseReleased(MouseEvent e){
  mouseAction("mouseReleased", e);
  }

  void mouseAction(String which, MouseEvent e){
  Component c = e.getComponent();
  if(e.isPopupTrigger()){
  JOptionPane.showMessageDialog(null,"Right Click");
  PopupMenu pm = getHash(c);
  pm.show(c, c.getSize().width/2, c.getSize().height/2);
  }
  }
  
  void addPopup(Component c, String name){
  PopupMenu pm = new PopupMenu();
  MenuItem mi = new MenuItem(name + "-1");
  mi.addActionListener(this);
  pm.add(mi);
  mi = new MenuItem(name + "-2");
  mi.addActionListener(this);
  pm.add(mi);
  setHash(c, pm);
  c.add(pm);
  c.addMouseListener(this);
  }
  
  Hashtable<Component,PopupMenu> popupTable = 
new 
Hashtable<Component,PopupMenu>();
  void setHash(Component c, PopupMenu p){
  popupTable.put(c, p);
  }
  
  PopupMenu getHash(Component c){
  return (PopupMenu)(popupTable.get(c));
  }
}

Download this example.