Hash Table for maintaining popup menus in Java

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


 

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

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.

Advertisements

Share on Google+Share on Google+

Hash Table for maintaining popup menus in Java

Posted on: April 16, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Hash Table for maintaining popup menus in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads