Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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.

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.